Middleware Laravel Group Authentication

Asked

Viewed 2,525 times

0

save guys, I want to know how to project my admin route so that the user does not have access to this route but they both have to be authenticated.

My routes

Route::group(['middleware' => ['auth']], function () {

    Route::get('/Admin', function () { //ADM ADM

        return "Seu ID: " . Auth::user()->id . " Você é ADM";

    })->name('admin');

    Route::get('/Usuario', function () { //USER USER

        return "Seu ID: " . Auth::user()->id . " Você é Usuario";

    })->name('usuario');

    Route::get('/semLogar', function () { // SEM LOGAR

        return "Você não está autenticado";

        })->name('semLogar');

    Route::get('/login/admin',['middleware' => 'Check:admin','uses' => 'AdminController@index', 'as' => 'indexAdm']);
});

My handler

public function index (){

    return "Opa controller adm";
}

Middleware

public Function Handle($request, Closure $next, $role) { if (!Auth::check()) {

    return redirect()->route('semLogar');

}
if(Auth::user()->role == $role){

    return redirect()->route('admin');

} else if (Auth::user()->role !== $role){

    return redirect()->route('usuario');
}

return $next($request);

}

middlewareGroups

'CheckGrupo' => [
        \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,
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'Check' => \App\Http\Middleware\CheckAdmin::class,
    ], 

routeMiddleware

    'Check' => \App\Http\Middleware\CheckAdmin::class,  

What I want is that when the user is logged in he could not put in the /admin URL and enter the /admin route that he can only enter if he is Adm.

NOTE: I am registering in the bank an ENUM('user','admin').

1 answer

2


You can define a group of routes and add middleware this way:

Route::middleware('auth')->group(function () {

   Route::middleware('Check')->group(function () {

     // Rotas que só o admin pode acessar
   });

   // Demais rotas
});

To the middleware of Check i would recommend the following code :

public function handle($request, Closure $next, $role)
{
    if (Auth::user()->role == $role) {

         return $next($request);
    } 

    return redirect()->route('usuario');   
}  

Like the middleware auth is executed before that middleware is not required to check if the user is logged in, and if you have more than one page for admin will not want to redirect it to route admin all the time.

Documentation link:

https://laravel.com/docs/5.5/routing#route-groups

  • vlw man helped GOD a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.