Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Auth::attempt() fails in a Passport API because the guard has no attempt method

Your error

BadMethodCallException: Method Laravel\Passport\Guards\TokenGuard::attempt does not exist.1 more line it can show

Fix it fast

Most common cause, 1 of 2

The default guard was switched to api

Put the default guard back on the session guard and name the api guard only where tokens are checked, with the auth:api middleware.

Fix
# .env
AUTH_GUARD=web

php artisan config:clear

// routes/api.php
Route::get('/user', fn (Request $request) => $request->user())->middleware('auth:api');
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

BadMethodCallException "Method Laravel\Passport\Guards\TokenGuard::attempt does not exist" means attempt() ran on Passport's api guard. That guard only reads bearer tokens; attempt() belongs to stateful session guards. Keep the default guard on web, check the password with Hash::check(), then return $user->createToken(...)->accessToken. Applies to Passport 13 on Laravel 12 and 13.

  • Applies to laravel/passport 13.12.0, Laravel 13.43 and 12 (PHP 8.3 to 8.5)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Look at the auth configuration Laravel has loaded. A default guard of api points to the first cause; a default of web means the code asks for the api guard by name.

Run this first
php artisan config:show auth

Then find the line you see in this list and open its fix:

  1. 1The default guard was switched to apiconfig:show auth shows defaults.guard as api, often from AUTH_GUARD=api in .env, and the login code calls plain Auth::attempt()
  2. 2The login code asks the api guard to check a passwordThe controller calls auth('api')->attempt(...) or Auth::guard('api')->attempt(...), often copied from a guide for a different token package

What does the error look like?

  • BadMethodCallException: Method Laravel\Passport\Guards\TokenGuard::attempt does not exist.
  • Method Laravel\Passport\Guards\TokenGuard::attempt does not exist.

Cause 1 of 2

The default guard was switched to api

You seeconfig:show auth shows defaults.guard as api, often from AUTH_GUARD=api in .env, and the login code calls plain Auth::attempt()

Check

Method TokenGuard::attempt does not exist appears when Auth::attempt() lands on the Passport guard. Auth::attempt() uses the default guard, and with the default set to api that is Passport's TokenGuard, which implements only the basic Guard contract. attempt() is declared on StatefulGuard, which the session guard implements, so the call falls through to the guard's macro handler and fails.

Check
grep -n '^AUTH_GUARD' .env

Fix

Put the default guard back on the session guard and name the api guard only where tokens are checked, with the auth:api middleware.

Fix
# .env
AUTH_GUARD=web

php artisan config:clear

// routes/api.php
Route::get('/user', fn (Request $request) => $request->user())->middleware('auth:api');

Verify

The default guard reads web, and an authenticated request to the API still passes with a bearer token.

Verify
php artisan config:show auth

Confirmed by laravel.com

Did this fix it?

Cause 2 of 2

The login code asks the api guard to check a password

You seeThe controller calls auth('api')->attempt(...) or Auth::guard('api')->attempt(...), often copied from a guide for a different token package

Check

Calling attempt() on the api guard by name raises the same exception. A Passport guard authenticates each request from the access token it carries and offers no attempt() and no session to log a user into, so the password check has to happen in your own code.

Check
grep -rn "guard('api')->attempt\|auth('api')->attempt" app/

Fix

Look the user up, compare the password against its stored hash, and issue a personal access token for that user. Create a personal access client once before the first token is issued.

Fix
php artisan passport:client --personal

// LoginController (use Illuminate\Support\Facades\Hash;)
$user = User::where('email', $credentials['email'])->first();

if (! $user || ! Hash::check($credentials['password'], $user->password)) {
    return response()->json(['message' => 'Invalid credentials'], 401);
}

return ['token' => $user->createToken('api')->accessToken];

Verify

The login route returns a token, and that token opens a route protected by auth:api.

Verify
curl -s -H "Authorization: Bearer $TOKEN" -H 'Accept: application/json' https://example.com/api/user

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Leave defaults.guard on web in config/auth.php and select the api guard per route with auth:api, never globally.
  • Keep login and token issuing in one controller with a feature test that posts valid and invalid credentials and asserts 200 and 401.
  • On Passport 13, prefer the authorization code flow for third-party clients; the docs no longer recommend the password grant, which also needs Passport::enablePasswordGrant().

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.