Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking which `guard` is loggedin

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)

like image 681
Miguel Avatar asked Aug 17 '16 14:08

Miguel


People also ask

What is Auth :: Guard?

The authenticate middleware allows you to specify what type of auth guard you want to use. Laravel 5.3 comes with two out of the box, 'web' and 'api'. The Auth facade uses the 'web' guard by default if none is specified. So for example: Auth::user() is doing this by default: Auth::guard('web')->user()

What is guard in laravel?

Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies. Providers define how users are retrieved from your persistent storage.

How can I change guard in laravel?

In this step we need to create our custom guard name. So visit config/auth. php and create your own guard name as many as you want. Now in this step we have to create our route for create Laravel multi auth using guard.


3 Answers

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.

like image 131
tomfrio Avatar answered Oct 17 '22 23:10

tomfrio


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

like image 32
miken32 Avatar answered Oct 18 '22 00:10

miken32


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.

like image 12
lewis4u Avatar answered Oct 18 '22 00:10

lewis4u