I am building a home service application with laravel for backend and react for frontend. So I use laravel sanctum for session-based authentication. In the future, I will probably need mobile application too. So, sanctum is the best choice possible.
I have two types of users. user and worker. They are in separate tables in database and I want to use multiple guards to separately authenticate them.
I have used the answer in this link and it does not work.
In my auth.php config file, I use these guards and providers:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'sanctum',
'provider' => 'users',
],
'worker' => [
'driver' => 'sanctum',
'provider' => 'workers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsUser::class,
],
'workers' => [
'driver' => 'eloquent',
'model' => AppModelsWorker::class,
],
],
But when I try to log in user in my controller:
auth('user')->login($user);
It gives me the following error:
"message": "Method Illuminate\Auth\RequestGuard::login does not exist.",
Actually, I tried to use default sanctum guard for user and log user in using ‘web’ guard:
auth('web')->login($user);
and It works, But an authenticated user can have access to the routes that need worker authentication:
Route::prefix('worker')->name('worker.')->middleware(['auth:worker'])->group(function () {
// ...
});
So what should I do?