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

noblemfd's avatar

Method Illuminate\Auth\RequestGuard::attempt does not exist while testing Laravel Passport restful api

I am using Laravel-8 for restful api, and it it I have Laravel Passport Authentication:

public function login(Request $request)
{
    $credentials = [
        'email' => $request->email,
        'password' => $request->password
    ];

    if (auth()->attempt($credentials)) {
        $token = auth()->user()->createToken('MyApp')->accessToken;
        return response()->json(['token' => $token], 200);
    } else {
        return response()->json(['error' => 'UnAuthorised'], 401);
    }
}

auth.api

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],
],

I have this route in api.php:

Route::group(['prefix' => 'v1/admin', 'as' => 'admin.', 'namespace' => 'Api\v1\Admin\', 'middleware' => ['api']], function () {
  Route::post('login', 'AdminLoginController@login');
});

When I tried to test on POSTMAN using POST Request, I got this error:

BadMethodCallException: Method Illuminate\Auth\RequestGuard::attempt does not exist. in file

How do I resolve this?

Thanks

0 likes
4 replies
Nakov's avatar

Only the session driver has attempt method, so you need to use the correct guard as your default one is the api

auth('web')->attempt($credentials)

this might work :)

1 like
noblemfd's avatar

@nakov - The error changed to:

Error: Call to a member function createToken() on null in file 

and it poits to this line:

$token = auth()->user()->createToken('MyApp')->accessToken;
Nakov's avatar
Nakov
Best Answer
Level 73

So it seems like the initial error is fixed now since you logged in the user you need to use the same guard again to create the token

$token = auth('web')->user()->createToken('MyApp')->accessToken;
1 like

Please or to participate in this conversation.