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

kevin73's avatar

Method Illuminate\Auth\RequestGuard::attempt does not exist.

Hi , i get this error message :

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

when i use Auth::guard('client') it works only with 'driver' => 'session' not passport i don't understand why so how to fix it ?

config/auth.php

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'administrator',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'administrator',
        ],
        'client' => [
            'driver' => 'passport',
            'provider' => 'client',
        ],
        'administrator' => [
            'driver' => 'passport',
            'provider' => 'administrator',
        ],

    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\administrator::class,
        ],
        'administrator' => [
            'driver' => 'eloquent',
            'model' => App\administrator::class,
        ],
        'client' => [
            'driver' => 'eloquent',
            'model' => App\Client::class,
        ]
    ]

];

app/Http/Controllers/Api/ClientController.php

public $successStatus = 200;
    /**
     * login
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request){
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required|min:6',
        ]);
        if ($validator->fails()) {
            return response()->json(['error'=>$validator->errors()], 401);
        }

        if(Auth::guard('client')->attempt(['email' => request('email'), 'password' => request('password')])){
            $user = Auth::guard('client');
            $success['token'] =  $user->createToken('MyApp')->accessToken;
            return response()->json(['success' => $success], $this->successStatus);
        }
        else{
            return response()->json(['error'=>'Email or password incorrect'], 401);
        }
    }

Thank you in advance

0 likes
9 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@kevin73 The attempt method is part of the SessionGuard you cannot use Passport, and attempt to log in a user, Passport is used for API authentication, and APIs typically use tokens to authenticate users and do not maintain session state between requests.

So on each request you should be using a token generated for your user, and do not try to log them in and make consecutive requests without the token.

Here is the RequestGuard for reference, so you can check that the method really is not there :)

If you want to keep this way, in your guard for the client, use the session driver instead:

'client' => [
   'driver' => 'session',
   'provider' => 'client',
],
8 likes
kevin73's avatar

done and when about it ?

BadMethodCallException
Method Illuminate\Auth\SessionGuard::createToken does not exist.

 public function login(Request $request){
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required|min:6',
        ]);
        if ($validator->fails()) {
            return response()->json(['error'=>$validator->errors()], 401);
        }

        $credentials = $request->only('email', 'password');

        if(Auth::guard('client')->attempt($credentials)){
            $user = Auth::guard('client');
            $success['token'] =  $user->createToken('MyApp')->accessToken;
            return response()->json(['success' => $success], $this->successStatus);
        }
        else{
            return response()->json(['error'=>'Email or password incorrect'], 401);
        }
    }
Nakov's avatar

@kevin73 well, don't call the createToken on a session guard instance :)

what about this:

$user = Auth::guard('client')->user();
$success['token'] =  $user->createToken('MyApp')->accessToken;
5 likes
axsax's avatar

Hey man, I can't understand why I have to choose session and not passport in a guard. Can u explain me please?

MuhammadRizki's avatar

does this method work even if you use "session" instead of "token"? please answer, thank you ..

rajaduraioec's avatar

@axsax @muhammadrizki Hello

I just found the solution for this.

You need to have two guards in auth.php

'clientapi' => [ 'driver' => 'passport', 'provider' => 'client', ],

'client' => [ 'driver' => 'session', 'provider' => 'client', ],

Now guard "clientapi" can be used in api.php for authentication

Route::group(['middleware' => 'auth:clientapi'], function () {

Route::get('something', 'API\SomeController@method');   
    

});

and guard "client" can be used to generate token

if(Auth::guard('client')->attempt(['email' => request('email'), 'password' => request('password')])) {

        $user = $this->guard()->user();
        $token              =  $user->createToken('token')->accessToken;
        // $result['user_id']  = $user->id;
        $result['success']  =  true;
        $result['message']  =  "Success! you are logged in successfully";
        $result['token']    =  $token;

        return response()->json(['result' => $result], $this->successStatus);
    }
3 likes
rajaduraioec's avatar

I just found the solution for this.

You need to have two guards in auth.php

'clientapi' => [
	 'driver' => 'passport', 
	'provider' => 'client',
 ]

'client' => [
	 'driver' => 'session', 
	'provider' => 'client', 
]

Now guard "clientapi" can be used in api.php for authentication

Route::group(['middleware' => 'auth:clientapi'], function () {
	Route::get('something', 'API\SomeController@method');  
});

and guard "client" can be used to generate token

if(Auth::guard('client')->attempt(['email' => request('email'), 'password' => request('password')])) {
     $user = $this->guard()->user();
     $token              =  $user->createToken('token')->accessToken;
     $result['user_id']  = $user->id;
     $result['success']  =  true;
     $result['message']  =  "Success! you are logged in successfully";
     $result['token']    =  $token;

     return response()->json(['result' => $result], $this->successStatus);
}

1 like
RamseyJiang's avatar

@nakov I also met this issue. My scenario is that I use Laravel sanctum and also use attempt login. The API login works, and the attempt also works for API. But when I try to do the unit test, it doesn't work. It will show "Method Illuminate\Auth\RequestGuard::attempt does not exist." When I try to do unittest the login. Do you have an idea to fix it? Many thanks, my bro.

2 likes

Please or to participate in this conversation.