Jwt-auth: Solu莽茫o: Laravel 6 tymon/jwt-auth:1.0.0-rc.5 Method Illuminate\Auth\SessionGuard::factory does not exist.

Created on 31 Dec 2019  路  6Comments  路  Source: tymondesigns/jwt-auth

config/auth.php

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

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

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

routes/api.php

Route::post('auth/login', 'Auth\JwtAuthController@login');
Route::group(['middleware' => 'api','prefix' => 'auth'], function () {
    Route::post('logout', 'Auth\JwtAuthController@logout');
    Route::post('refresh', 'Auth\JwtAuthController@refresh');
    Route::post('me', 'Auth\JwtAuthController@me');
});

AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

class JwtAuthController extends Controller
{
    public function login() {
            $credentials = request(['email', 'password']);
            if (! $token = auth('api')->attempt($credentials)) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }
        return $this->respondWithToken($token);
    }

    public function logout() {
        auth('api')->logout();
        return response()->json(['message' => 'Successfully logged out']);
    }

    public function refresh() {
        return $this->respondWithToken(auth('api')->refresh());
    }

    protected function respondWithToken($token)  {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 60
        ]);
    }

    public function me() {
    return response()->json(auth('api')->user());
    }
}

Most helpful comment

From the docs:

Multiple Guards
If the newly created 'api' guard is not set as a default guard 
or you have defined multiple guards to handle authentication, 
you should specify the guard when calling auth().

  $token = auth('api')->attempt($credentials);

All 6 comments

@SouKBit per the docs, you need to adjust your auth.php like so:

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

From the docs:

Multiple Guards
If the newly created 'api' guard is not set as a default guard 
or you have defined multiple guards to handle authentication, 
you should specify the guard when calling auth().

  $token = auth('api')->attempt($credentials);

Add "implements JWTSubject" in your user class.

class User extends Authenticatable implements JWTSubject

Solved for me, trying for this example:

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

That line is the problem for the laravel 5.8 ^.

'hash' => false,

Method Illuminate\Auth\SessionGuard::factory does not exist.

If you confirm there are no wrong on your project to implement jwt that time you can do it...

php artisan config:cache
php artisan cache:clear
php artisan route:clear

.. it's work for me...

implements JWTSubject

and use Tymon\JWTAuth\Contracts\JWTSubject;

eg

...
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heroghost picture heroghost  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments

lloy0076 picture lloy0076  路  3Comments

gandra picture gandra  路  3Comments

CBR09 picture CBR09  路  3Comments