Framework: Dynamicaly change auth guard

Created on 9 Jun 2017  路  4Comments  路  Source: laravel/framework

How i can change dynamicaly my auth guard ?

For example if route prefix is /admin i need use admin auth guard
and if route prefix is /customer i need use customer auth guard?

Now i need write some like this

Auth::guard('admin')->user()

Most helpful comment

Create middleware to make this happen, you can change default guard on the fly $this->app['auth']->setDefaultDriver('admin');.

Solution ready to cmd+c && cmd+v: http://mattallan.org/2016/setting-the-guard-per-route-in-laravel/

Next time search.

All 4 comments

Create middleware to make this happen, you can change default guard on the fly $this->app['auth']->setDefaultDriver('admin');.

Solution ready to cmd+c && cmd+v: http://mattallan.org/2016/setting-the-guard-per-route-in-laravel/

Next time search.

@Kyslik Very useful snippet! Thank you.

You can also use the shouldUse() method on the AuthManager class. Then, in your controller, call Auth::shouldUse('guardName'); Then, all future calls to auth() will use that guard.

@gopal-g I just got the notification from GitHub about this now 馃ぃ .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lzp819739483 picture lzp819739483  路  3Comments

felixsanz picture felixsanz  路  3Comments

YannPl picture YannPl  路  3Comments

shopblocks picture shopblocks  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments