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
- Change auth() to auth('api') for ->attempt and ->factory() as per https://github.com/tymondesigns/jwt-auth/issues/1484 to get the JWT token working.. — committed to pdiveris/sixacts by pdiveris 5 years ago
- JWT認証機能の変更 公式に則ってやりやした。 auth()をauth('api')に — committed to yumauma20/intern_review by yumauma20 4 years ago
I fixed it changing this protected function: respondWithToken
replace
'expires_in' => auth()->factory()->getTTL() * 60by'expires_in' => auth('api')->factory()->getTTL() * 60and 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.
Thanks @faxterol, it worked. If you still have problems, cancel
php artisan serveand then: Si aún tienen problemas, cancelenphp artisan servey luegoin 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
Replacing
'expires_in' => auth()->factory()->getTTL() * 60solved the problem for meRemember to use
apiguardand the refresh function obviously :
I was having the issue after change all above stuff, as clear the cache, it works ❤️ thanks.
thanks 😃 it works…