Jo Micheal
Published in : 2022-02-18
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!
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now
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
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