Skip to content
Advertisement

I can’t get JWT token in Laravel

I am creating an API in Laravel 8 (PHP 7.4) and I wanted to secure it. The choice fell on tymon / jwt-auth. I follow the documentation from the website: https://jwt-auth.readthedocs.io/en/develop/quick-start/

Everything works fine here. However, later when I create a route to login and want to get the token using auth()->attempt(), I have the following error:

Undefined method ‘attempt’.

My Route code:

Route::get('/login', function () {
    $loginData = request()->only(['email', 'password']);

    $token = auth()->attempt($loginData);

    return $token;
});

auth.php:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

What’s wrong?

Advertisement

Answer

You can try this line

 $token = auth('api')->attempt($loginData);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement