check multiple guard in Laravel controller
Asked Answered
N

1

7

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?

Nonconductor answered 17/4, 2018 at 6:59 Comment(0)
N
17

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.

Nonconductor answered 17/4, 2018 at 7:0 Comment(2)
{{ 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
Can't we simply use Auth::user()->name?Parkerparkhurst

© 2022 - 2024 — McMap. All rights reserved.