Checking which `guard` is loggedin

52,295

Solution 1

One way to do this is to extend the Laravel authentication class in the IoC container to include, for instance, a name() method that check which guard is used for the current session, and calls user() on that Guard instance.

Another way is to simply use an if-statement in your Blade template:

@if(Auth::guard('admin')->check())
    Hello {{Auth::guard('admin')->user()->name}}
@elseif(Auth::guard('user')->check())
    Hello {{Auth::guard('user')->user()->name}}
@endif

However, this is a little dirty. You can clean this up a bit by using a partial, or by passing the view a variable containing the guard name, either directly from your Controller, or via a ViewComposer, and then doing:

Hello {{Auth::guard($guardName)->user()->name}}

in your View.

Extending Laravel's authentication is your best option, imo.

Solution 2

This will get the guard name that is used for current logged in user

Auth::getDefaultDriver()

When you log in, by default it will get you the:

'web'

Dependable through which guard you've been logged in it will get you that guard name.

This is not applicable for APIs!!! Because APIs in laravel by default don't use session.

Solution 3

Since Laravel 5.5, this is easy to do with the @auth template directive.

@auth("user")
    You're a user!
@endauth

@auth("admin")
    You're an administrator!
@endauth

@guest
    You're not logged in!
@endguest

Reference: https://laravel.com/docs/5.6/blade#if-statements

Solution 4

I recommend to use global helper function like

function activeGuard(){

foreach(array_keys(config('auth.guards')) as $guard){

    if(auth()->guard($guard)->check()) return $guard;

}
return null;
}

Solution 5

Depends of Harat answer, I built a Class names CustomAuth, and it give me easy access to Auth facade methods: user() and id().

<?php

namespace App\Utils;

use Illuminate\Support\Facades\Auth;

class CustomAuth{
    static public function user(){
        return Auth::guard(static::activeGuard())->user() ?: null;
    }

    static public function id(){
        return static::user()->MID ?: null;
    }

    static private function activeGuard(){

        foreach(array_keys(config('auth.guards')) as $guard){

            if(auth()->guard($guard)->check()) return $guard;

        }
        return null;
    }
}

Share:
52,295
Miguel
Author by

Miguel

Updated on September 25, 2021

Comments

  • Miguel
    Miguel over 2 years

    I have a multiauth laravel 5.2 app, with the fallowing guards defined on config/auth.php:

    ...
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
    'user' => [
        'driver' => 'session',
        'provider' => 'user',
    ],
    ...
    

    So, admin and user.

    The problem resides in the view layer, since this two loggedin guards share some views, ex:

    Hello {{Auth::guard('admin')->user()->name}}
    

    In this case the guard is hardcoded into the view to be always admin (it gives error when loggedin guard is user), but, to avoid have to do another equal view just for this little change, I would like have it dinamic, something like:

    Hello {{Auth::guard(<LOGGEDIN GUARD>)->user()->name}}
    

    PS: I know that this could be achieved getting the corresponding url segment, ex: www.site.com/pt/user/dasboard which in the case it would be segment 2, but this way the app would lose scalability, since in the future the corresponding segment may not be the same (2 in the example above)

  • Miguel
    Miguel over 7 years
    Thanks but that also would remove scalability from the app since the I would be fixed to the admin/user guard
  • tomfrio
    tomfrio over 7 years
    Very true. That's why extending the Laravel authentication is the better option here.
  • adam78
    adam78 almost 7 years
    @tomfrio what if you're logged as both?
  • the manh Nguyen
    the manh Nguyen over 5 years
    i tried with your answer, but when i login with auth('admin'),it show content of admin but it still shows content of guest, i dont know why?!
  • saber tabatabaee yazdi
    saber tabatabaee yazdi over 4 years
    return me : auth guard [user] is not defined ... but [admin] worked and return false for non admin new user
  • Shiro
    Shiro over 3 years
    Auth::getDefaultDriver() is get $this->app['config']['auth.defaults.guard']; it should be always the config value
  • lewis4u
    lewis4u over 3 years
    Not if you have multiple and are used dynamically... with that ^ you will get the default that is hard coded in config.auth file
  • Mladen Janjetovic
    Mladen Janjetovic almost 3 years
    This is the default driver from the config, not the the one that is used for current logged user