save folks, I want to know how to design 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 controler
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 URL / admin and enter the route / admin that he can only enter if it is adm.
NOTE: I am registering an ENUM ('user', 'admin') in the database.