Search code examples
phplaraveljwtdingo-api

Laravel JWT Auth error


I'm trying to implement JWT in Laravel 5.2 but I get this error:

"message": "call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\\Auth\\TokenGuard' does not have a method 'once'",
  "status_code": 500,
  "debug": {
    "line": 288,
    "file": "/home/vagrant/Code/lsupport/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php",
    "class": "ErrorException",

My routes file:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1',function($api)
{
    $api->post('login','App\Http\Controllers\Auth\AuthController@authenticate');
});

My AuthController:

 public function authenticate(Request $request)
    {
        $credentials = $request->only('email','password');

        try {
            if(!$token = JWTAuth::attempt($credentials)) {
                return $this->response->error(['error' => 'User credentials are not correct!'],401);
            }
        } catch(JWTException $ex) {
            return $this->response->error(['error' => 'Something went wrong!'],500);
        }
        return $this->response->item(compact('token'));
    }

I'm testing with postman.


Solution

  • Also had the same issue, I solved it by setting my default guard to 'web' in the auth.php file inside config folder.

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    

    Remember, your route should not have the auth middleware for this login, because, this is just to authenticate.