Here is an example of Laravel command:
class TestUserCommand extends Command
{
protected $signature = 'test:user';
protected $description = 'Command description';
public function handle(): void
{
auth('api')->loginUsingId(1);
... domain service call
}
}
It gives me the following error:
Method IlluminateAuthRequestGuard::loginUsingId does not exist.
What I want to do is authenticate so I can then initiate some complex business logic, that internally retrieves current user like so:
return auth('api')->check() ? auth('api')->user() : null;
Here is my auth.php configuration:
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
UPD: What worked for me as temporary solution is Passport::actingAs()
, but it does not seem to be meant for production.
2
Answers
loginUsingId
does not exists on theRequestGuard
, that’s a method on theSessionGuard
and you are usingapi
auth here, in that case it won’t work. Better you useweb
auth instead of api in your case, please try below code:and to check if you are authenticated or not, try this:
Try with
Auth::attempt()
and use prompt to get mail and password like,Otherwise with Arguments like,
Inside handle method, you will get inputs with argument method
Hope this may help you.