Error while creating MultiAuth Api in Laravel 8 using Guards

  api, authentication, laravel, php

Error

This is the error I have been receiving while using Postman

BadMethodCallException: Method IlluminateAuthRequestGuard::attempt does not exist. in file C:xampphtdocsproject-appvendorlaravelframeworksrcIlluminateMacroableTraitsMacroable.php on line 103

I have two tables users and employees and my login API is only working for users table but not for employees table.

This is my EmployeeController

public function employeeLogin(){
if(Auth::guard('employee-api')->attempt(['email' => request('email'), 'password' => request('password')])){
    $user = Auth::guard('employee-api')->user();
    $success['token'] =  $user->createToken('MyApp')->accessToken;
    return response()->json(['success' => $success], $this->successStatus);
}
else{
    return response()->json(['error'=>'Unauthorised'], 401);
}

}

api.php


    Route::middleware(['auth:api', 'auth:employee-api'])->group(function () {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });

    Route::get('/employee', function () {
        return $request->employee();
    });
});

Route::post('login', '[email protected]');

Route::post('employeeLogin', '[email protected]');

config/auth.php

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

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

    'employee-api' => [
    'driver' => 'passport',
    'provider' => 'employees',
    ],
],

Source: Laravel

Leave a Reply