BadMethodCallException: Method LaravelPassportGuardsTokenGuard::attempt does not exist. in file C:xampphtdocstestpassportapilaravel-backend-apivendorlaravelframeworksrcIlluminateMacroableTraitsMacroable.php on line 112
auth()->guard(‘api’)->attempt($request->only([’email’, ‘password’])))
This is supported passport or not,
because this error shows at my end.
How to solve this?
config->auth.php `page`
'company' => [
'driver' => 'session',
'provider' => 'company_users', // Use the appropriate provider
],
'api' => [
'driver' => 'passport', // set this to passport
'provider' => 'company_users',
'hash' => false,
]
if (Auth::guard('api')->attempt($request->only(['email', 'password']))) {
$user = Auth::guard('api')->user();
$token = $logUser->createToken('MyApp')->accessToken;
Its a not working
2
Answers
Laravel
passport
(TokenGuard
) doesn’t have theattempt()
method. It uses theOAuth2
token, not thesession
.As you shared, you create your token like this.
Then in the config
And finally, in your request, if you pass the
Authorization
header with theBearer
, this will work.The issue is that
attempt
is a method ofSessionGuard
but notTokenGuard
.If you want to authenticate a user before creating a token for them use the
web
guard.