I create three types of guards in laravel, I can check auth users but we have some route and controller that is same for all users and I need to check all guards in the same routes or controllers for each type of users that log in.
my auth.php file looks like this
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'student' => [
'driver' => 'session',
'provider' => 'students',
],
'finance' => [
'driver' => 'session',
'provider' => 'finances',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
'finance-api' => [
'driver' => 'token',
'provider' => 'finances',
],
],
'providers' => [
'students' => [
'driver' => 'eloquent',
'model' => App\student::class,
],
'finances' => [
'driver' => 'eloquent',
'model' => App\Finance::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\users::class,
],
'finance' => [
'driver' => 'eloquent',
'model' => App\users::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'students' => [
'provider' => 'students',
'table' => 'password_resets',
'expire' => 60,
],
'finances' => [
'provider' => 'finances',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 15,
],
'finance' => [
'provider' => 'finance',
'table' => 'password_resets',
'expire' => 15,
],
],
];
How I can check multiple guards in the same controller or route?
{{ Auth::guard('admin')->user()->name }}{{ Auth::guard('admin')->user()->last_name }}
this will raise an error if the logged user was authenticated using different guard. You can save the guard used to authenticate the user in the session and use it to check again{{ Auth::guard(Session('guard'))->user()->name }}
– Noe