Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farshadf's avatar

how to login the user with specific guard like api in laravel passport

hi guys , in my project i have a part that i want to manually logs in a user in my api guard , now what i have tried is that :

auth()->guard('api')->attempt($mobile,$password);

but i get this error :

"Method Illuminate\Auth\RequestGuard::attempt does not exist.", "exception": "BadMethodCallException", "

i tried to use :

    $user = Auth::login($user);

but this logs the user in into web guard and not the api guard . another way i used the :

dd($user->createToken($user->name));

i have the token now and i wanted to know if its possible to logs the user into guard('api') and have it authenticated . thanks in advance

0 likes
5 replies
rodrigo.pedra's avatar
Level 56

Try this:

auth()->guard('api')->setUser($user);

attempt() is only available to guards implementing the StatefulGuard interface.

3 likes
farshadf's avatar

thanks , thats works really well , You Saved a Day .

1 like

Please or to participate in this conversation.