user image

Jo Micheal
Published in : 2022-02-18

Method Illuminate\Auth\RequestGuard::attempt does not exist.

Laravel

I am facing this bug 

Method Illuminate\Auth\RequestGuard::attempt does not exist.

when I try to use Multi Auth in my Laravel app using Laravel Passport with APIs (Laravel 9)

my auth.php

'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'regular_user_api' => [ 'driver' => 'passport', 'provider' => 'users', 'hash' => true, ],], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ],

The login controller

 public function login(Request $req){ $validator = Validator::make($req->all(), [ 'email' => 'required|email', 'password' => 'required', ]); if ($validator->fails()) { return response()->json($validator->errors(), 400); } if(Auth::guard('regular_user_api')->attempt(['email' => request('email') , 'password' => request('password')])){ config(['auth.guards.api.provider' => 'user']); $token = Auth::guard('regular_user')->user()->createToken('auth_token')->accessToken; return response()->json([ "user" => Auth::guard('regular_user')->user(), "token" => $token ], 200); } else { return response()->json(['Error'], 400); } }

How can I fix this bug? anyone help!

 

Comments

Mohamed Atef Date : 2022-02-18

Best answers

51

Best answers

51

You will not be able to use the guard on the “token” or “passport” guard, so the only way to do it is to use "session" but here is how you can figure it out, you will need to repeat your guard one time will be with "session" driver and another time will be using “passport” but both of them will have the same provider but with different names so your guards array should looks like 

'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'regular_user' => [ 'driver' => session', 'provider' => 'users', 'hash' => true, ], 'regular_user_api' => [ 'driver' => 'passport', 'provider' => 'users', 'hash' => true, ],],

and in the login function, you will use the one which contains the session, so you can pass this error and because they got the same provider it will be working. here is example
Good luck

Jo Micheal Date : 2022-02-18

Thank you so much this is very helpful

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Laravel API POST method returns 419
Publish date: 2022-08-03 | Comments: 1
Get user location (city - country) from his IP address
Publish date: 2022-03-02 | Comments: 2
tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0
Publish date: 2022-02-18 | Comments: 2
Increase Sanctum token length in Laravel x9
Publish date: 2022-07-16 | Comments: 2
How can I use arabic words, paragraphs in the faker?
Publish date: 2022-03-06 | Comments: 1
How can i pass data from blade template to vue component?
Publish date: 2022-02-11 | Comments: 1