Jwt-auth: Method factory does not exist.

Created on 16 Feb 2018  路  17Comments  路  Source: tymondesigns/jwt-auth

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

Most helpful comment

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

All 17 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

@faxterol Thanks

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

and the refresh function obviously :

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

Yes, Changing "auth()" by "auth('api')" in all auth controller for jwt.

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 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)

This does the trick when you do not want the "api" as a default guard.

Also, you can add this to your LoginController.

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

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..

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 <3 thanks.

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);
    }

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)

WOrked

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 m8. 馃帀馃帀馃帀馃帀馃帀馃帀馃帀馃帀

I needed make the change for everything in AuthController related to JWT ( basically everything except me() and logout() ).

No changes required to jwt.php though.

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)

work's for me.
Thanks

In the guards > api, remember remove hash key, for me not working con that key.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lbottoni picture lbottoni  路  3Comments

hfalucas picture hfalucas  路  3Comments

heroghost picture heroghost  路  3Comments

therealmjk picture therealmjk  路  3Comments

mihailo-misic picture mihailo-misic  路  3Comments