-1

recently i created two middlewares which is one for user called device, and one other for super user which is high level of admin. This is my middleware

Role Device Middleware

<?php

namespace App\Http\Middleware;

use Closure;

class RoleDevice
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check() && Auth::User()->role=='device'){
            return $next($request);
        }
        return redirect()->route('login')->with('danger',"You don't have an access");
    }
}

Role Device Super User

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use User;
class RoleSuper
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check() && Auth::User()->role=='super'){
            return $next($request);
        }
        return redirect()->route('login')->with('danger',"You don't have an access");
    }
}

after i created the middlewares, i put into the routes which is one route could access two middlewares. Here is one of my route.

Route::get('/dashboard','DashboardController@index')->middleware(['rolesuper','roledevice'])->name('dashboard');

and when i try to log in into my website, it returns

You don't have an access

which is don't pass into the middleware.

i hope i get any comments above ! thanks.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Abdan Syakuro
  • 1,034
  • 2
  • 12
  • 26

1 Answers1

2

Middlewares are executed in the order the are passed. So in case first middleware returns redirect response that's it - second middleware won't be executed.

You could combine both middleware into one and pass available roles as middleware parameter or just create single middleware for this that will verify if user is authorized.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291