问题
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?
回答1:
For each controller that you want to authentication
public function __construct()
{
$this->middleware('auth:admin,student,finance');
}
OR you can use in the route
Route::group(['middleware' => 'auth:admin,finance,student'], function () {
any same route here
});
And you can access to users details in blade like this
{{ Auth::guard('admin')->user()->name }} {{ Auth::guard('admin')->user()->last_name }}
I Hope this helps some one.
来源:https://stackoverflow.com/questions/49871765/check-multiple-guard-in-laravel-controller