jwt-auth: Method factory does not exist.

getting this error when logging in. Method factory does not exist.

AuthController

class AuthController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }

    public function login(Request $request)
    {

        $credentials = $request->only('email', 'password');
        if (! $token = Auth::guard('api')->attempt($credentials)) {
               return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }
...
}

auth.php


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

  

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

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

Setting default guard to api fix the issue but I want default guard to be the web

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 20
  • Comments: 18

Commits related to this issue

Most upvoted comments

I fixed it changing this protected function: respondWithToken

replace 'expires_in' => auth()->factory()->getTTL() * 60 by 'expires_in' => auth('api')->factory()->getTTL() * 60

and in the config/jwt.php change by jwt, auth and storage key values by this:

‘jwt’ => ‘Tymon\JWTAuth\Providers\JWT\Namshi’, ‘auth’ => ‘Tymon\JWTAuth\Providers\Auth\Illuminate’, ‘storage’ => ‘Tymon\JWTAuth\Providers\Storage\Illuminate’

Sources: https://github.com/tymondesigns/jwt-auth/issues/1404#issuecomment-362944871 https://github.com/tymondesigns/jwt-auth/issues/1326#issuecomment-335665499

Yes, Changing “auth()” by “auth(‘api’)” in all auth controller for jwt.

Also, you can add this to your LoginController.

    public function __construct()
    {
        return auth()->shouldUse('api');
    }

Thanks @faxterol, it worked. If you still have problems, cancel php artisan serve and then: Si aún tienen problemas, cancelen php artisan serve y luego

php artisan cache:clear
php artisan config:cache

in config/auth.php change defualt guard from web to api ‘defaults’ => [ ‘guard’ => ‘api’, ‘passwords’ => ‘users’, ],

I have the same issue (the workaround of using defaults guard api works indeed but it’s not logical).

The documentation (on mkdocs) should be updated with the example that gives @faxterol . It seems although that changing the config/jwt.php is useless, just modify the function:

respondWithToken

'expires_in' => auth('api')->factory()->getTTL() * 60

Replacing 'expires_in' => auth()->factory()->getTTL() * 60 solved the problem for me

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

Remember to use api guard

public function login()
    {
        $credentials = request(['email', 'password']);

        if (! $token = auth()->guard('api')->attempt($credentials)) {
            return response()->json(['error'=>true, 'message'=>'Invalid Credentials']);
        }

        return $this->respondWithToken($token);
    }

and the refresh function obviously :

return $this->respondWithToken(auth('api')->refresh());

Thanks @faxterol, it worked. If you still have problems, cancel php artisan serve and then: Si aún tienen problemas, cancelen php artisan serve y luego

php artisan cache:clear
php artisan config:cache

I was having the issue after change all above stuff, as clear the cache, it works ❤️ thanks.

I fixed it changing this protected function: respondWithToken

replace 'expires_in' => auth()->factory()->getTTL() * 60 by 'expires_in' => auth('api')->factory()->getTTL() * 60

and in the config/jwt.php change by jwt, auth and storage key values by this:

‘jwt’ => ‘Tymon\JWTAuth\Providers\JWT\Namshi’, ‘auth’ => ‘Tymon\JWTAuth\Providers\Auth\Illuminate’, ‘storage’ => ‘Tymon\JWTAuth\Providers\Storage\Illuminate’

Sources: #1404 (comment) #1326 (comment)

thanks 😃 it works…