Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check multiple guard in Laravel controller

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?

like image 817
Nasser Ali Karimi Avatar asked Apr 17 '18 06:04

Nasser Ali Karimi


1 Answers

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.

like image 117
Nasser Ali Karimi Avatar answered Oct 28 '22 20:10

Nasser Ali Karimi