1

i want to view dashboard when logged in as both "admin" and "jeweller". When i log in a "admin" its redirecting to dashboard, when when i log in as jeweller, its not going to dashboard. I have 2 types of admin & tables ie.. "admin", "jeweller"

In auth.php , i have created 2 types of providers

'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'jeweler' => [ 'driver' => 'session', 'provider' => 'jewelers', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], 'jewelers' => [ 'driver' => 'eloquent', 'model' => App\Jeweler::class, ], ], 

i have declared a middleware called "AdminAuthenticated" , in AdminAuthenticated.php we have

class AdminAuthenticated { public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return $next($request); } return redirect()->route('admin.login'); } } 

and in web.php we have

Route::group(['prefix' => 'admin','middleware'=> ['adminAuth:admin','adminAuth:jeweler']], function () { }); 

and in kernal we have

protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], ]; protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'adminAuth' => \App\Http\Middleware\AdminAuthenticated::class, ]; 
5
  • You can add a middleware group in your $middlewareGroups array e.g. 'admin' =>['adminAuth:admin','adminAuth:jeweler'] and set your group middleware to admin Commented Nov 28, 2017 at 11:04
  • You should take a look at \Illuminate\Auth\Middleware\Authenticate middleware and compare your own AdminAuthenticated middleware if they are both doing the same logic for authenticating. Commented Nov 28, 2017 at 11:09
  • @apokryfos .. i tried this, but result is same, cant login with jeweler Commented Nov 28, 2017 at 11:25
  • 1
    If you need one of the two middleware to pass then you should combine them into a AuthWithAdminOrJewler middleware. Laravel doesn't "or" the middleware, all must pass. Commented Nov 28, 2017 at 11:26
  • @apokryfos.. thanks that worked Commented Nov 28, 2017 at 11:42

2 Answers 2

3
class AdminAuthenticated { public function handle($request, Closure $next, ...$guards) { if ($guards) { foreach ($guards as $guard) { if (!Auth::guard($guard)->check()) { return redirect()->route('admin.login'); } } return $next($request); } return redirect()->route('admin.login'); } } 

use

Route::group(['prefix' => 'admin','middleware'=> ['adminAuth:admin,jeweler']], function () { }); 
Sign up to request clarification or add additional context in comments.

3 Comments

i tried this, but result is same, cant login with jeweler
modified your answer to ...... if ($guards) { $guards = explode('-', $guards); foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { return $next($request); } } } return redirect()->route('admin.login'); and root to ,'middleware'=>['adminAuth:jeweler-admin']
This middleware will fail if the user isn't authenticated on all the supplied guards. I thought the problem was that OP wanted the user to be authenticated on at least one guard.
0

You can do this to check one of the guards:

class AdminAuthenticated { public function handle($request, Closure $next, $guard = null) { if (func_num_args() == 2 && \Auth::check()) { //Default guard return $next($request); } for ($i = 2; $i < func_num_args();$i++) { if (Auth::guard(func_get_arg($i))->check()) { return $next($request); } } return redirect()->route('admin.login'); } } 

This middleware will pass if one of the specified guards passes or if the default guard passes when there's no parameters.

You can use it like:

Route::group([ 'prefix' => 'admin', 'middleware' => 'adminAuth:admin,jewler' ], function () { ... 

2 Comments

if ($guards) { $guards = explode('-', $guards); foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { return $next($request); } } } return redirect()->route('admin.login'); and root to ,'middleware'=>['adminAuth:jeweler-admin']
That would also work but if you comma-separate them then they'll be passed as separate parameters to the middleware instead of a single parameter and will be in the func_get_args() array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.