1
votes

I am using Laravel 7 and I have passport installed as a dependency. When I make a login/ register request, it fails with the error

{ "message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", "file": ".../vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php", "line": 43, "trace": [ { "file": "/Users/macbookpro/.../vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php", "line": 162,...

My route looks like this:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


Route::post('api/v1/login', 'API\UserController@login');
Route::post('api/v1/register', 'API\UserController@register');
Route::group(['middleware' => 'auth:api'], function(){
//Route::post('details', 'API\UserController@details');
});

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

My controller looks like this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    
    public $successStatus = 200;
    
    public function login(Request $request){ 
        //dd('hello');
        return response()->json(['error'=> 'Mike'], 500);
        if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){ 
            $user = Auth::user(); 
            //$email = request
            $success['token'] =  $user->createToken('MyApp')-> accessToken; 
            return response()->json(['success' => $success], $this-> successStatus); 
        } 
        else{ 
            return response()->json(['error'=>'Unauthorised'], 401); 
        } 
    }

    public function register(Request $request) 
    { 
        $validator = Validator::make($request->all(), [ 
            'name' => 'required', 
            'email' => 'required|email', 
            'password' => 'required', 
            'c_password' => 'required|same:password', 
        ]);
if ($validator->fails()) { 
            return response()->json(['error'=>$validator->errors()], 401);            
        }
$input = $request->all(); 
        $input['password'] = bcrypt($input['password']); 
        $user = User::create($input); 
        $success['token'] =  $user->createToken('MyApp')-> accessToken; 
        $success['name'] =  $user->name;
return response()->json(['success'=>$success], $this-> successStatus); 
    }
    
...    

Here is my "POST" request via postman:

http://localhost:8000/api/v1/[email protected]&password=password

When I changed my route configuration like this:

Route::post('api/v1/login',  dd('hello'));

I got the corresponding "hello" on postman.

1
So this return response()->json(['error'=> 'Mike'], 500); is not working, right? - Christophe Hubert
@ChristopheHubert No, it is not working - mykoman
that dd('hello') right on your routes will get executed even before you'd hit the endpoint. Try putting that dd() or your whole Controller logic in a closure right in your routes to verify. - doesnotmatter
@doesnotmatter when I put the dd() within the login(), the former error persisted. - mykoman

1 Answers

0
votes

I finally found the cause of the problem. It appears that laravel/passport uses "api" by default for routes in api.php files. The routes is supposed to look like this:

...

Route::post('v1/login', 'API\UserController@login');
Route::post('v1/register', 'API\UserController@register');

...