in my project I have two guards, the first is "web" related to admins table with session driver, and the second one is "mobile" related to users table with token driver.
'defaults' => [
'guard' => 'web',
'passwords' => 'admins',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'admins',
],
'mobile' => [
'driver' => 'token',
'provider' => 'users',
],
],
Note: I am using sanctum authentication system.
so I am trying to log in using the second guard and when I attempt credentials, an error occurs:
public function login(Request $request)
{
.............
if(!Auth::guard('mobile')->attempt($request->only(['email', 'password']))){
return response()->json([
'status' => false,
'message' => 'Email & Password does not match with our record.',
], 401);
}
.............
}
the error:
"Call to undefined method IlluminateAuthTokenGuard::attempt()"
So how can I check the credentials of non-default guard, or if there is any suggestion ?
2
Answers
@sanjog-karki your solution worked for me, but I tried another solution which is by duplicating the "mobile" guard and setting the driver of the new guard to be: "session", so I can use Auth::guard("new-guard")->attempt($credintials) without any problem.
so now I can check the credentials like this:
and I can use it in middleware like this:
I will stick with this solution unless something went wrong.
if you are using sanctum then you can do like this