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

masumrahmanhasan's avatar

Authentication error while using santum driver

I was creating an application which authentication was used with sanctum when i try to submit the login it gives me Method Illuminate\Auth\RequestGuard::attempt does not exist. in my auth.php file i have ` 'defaults' => [ 'guard' => env('AUTH_GUARD', 'web'), 'passwords' => env('AUTH_PASSWORD_BROKER', 'users'), ],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| which utilizes session storage plus the Eloquent user provider.
|
| All authentication guards have a user provider, which defines how the
| users are actually retrieved out of your database or other storage
| system used by the application. Typically, Eloquent is utilized.
|
| Supported: "session"
|
*/

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

and in my controller i have

public function login(LoginRequest $request): \Illuminate\Http\JsonResponse
    {
        if(!$user = Auth::guard('api')->attempt($request->validated())) {
            return $this->failed(message:'Invalid Credentials! Please check and try again');
        }

        if($this->authenticatedUser()->status->isStatusInactive()) {
            return $this->failed(message:'Your account is not active!');
        }
        
        if(!$this->authenticatedUser()->email_verified_at) {
            return $this->failed(message:'Please verify your email address!');
        }

        if($this->authenticatedUser()->status->isStatusExpired()) {
            return $this->failed(message:'Your account is expired!Please contact administrator');
        }

        if($this->authenticatedUser()->status->isStatusSuspended()) {
            return $this->failed(message:'Your account is suspended!Please contact administrator');
        }

        if($this->authenticatedUser()->status->isStatusBanned()) {
            return $this->failed(message:'Your account is banned!');
        }
        
        return $this->success($this->tokenResponse($this->authenticatedUser()), message: 'Logged in successfully');
    }

but it still gives me the same error i want to use the sanctum with sanctum driver not session driver

0 likes
2 replies

Please or to participate in this conversation.