Check which of the 'guards' is loggedin

1

I have a large application 5.2, with multiauthentication, the guards configured in config/auth.php are:

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

That is admin and user .

My problem is in the view, because these two guards when loggedin share some same views, that's where the problem is generated:

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

In this case the corresponding guard is hardcoded to always be the admin (giving error if it is the% guard that is loggedin), after some search I did not find anything to indicate the solution to me, that is, to know if the guard that is loggedin is user or user , this way it would excuse another view equal only with this change. Solution EX:

Olá {{Auth::guard(<GUARD QUE ESTEJA LOGIN>)->user()->name}}

PS: I know I would go through the url segment corresponding to the guard, eg: admin , in this case it would be segment 2, but you would lose one of the app scalability because nothing guarantees that in the future the corresponding segment of the url continue to be 2.

    
asked by anonymous 17.08.2016 / 15:50

1 answer

4

Dude, you've thought about it:

$guard    = $this->getGuard();
$provider = $guard->getProvider();
Auth::guard($provider)->get()->name;

Otherwise, something you can do:

if(Auth::guard('admin')->check()){
   Auth::guard('admin')->admin()->name;
}
elseif(Auth::guard('user')->check()){
   Auth::guard('user')->user()->name;
}
    
19.08.2016 / 16:13