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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

{{ 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 }}
Can't we simply use Auth::user()->name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.