I'm using Laravel 5 on a Windows dev machine. I want to customize and use the Auth middleware throughout my application, to maintain authentication. My use case is a standard one. There are two (or three) classes of users - Admin and Regular (regular would be all users that are not admin).
The Admin has the obvious role of backend management, and hence has a separate routing group /admin/, which should redirect an unlogged user to /admin/login. I have set it up like so..
Route::group(['middleware'=>'auth', 'prefix' => 'admin'], function() {
Route::get('login','App\AuthController@getLogin');
Route::post('login','App\AuthController@postLogin');
});
When the login form is posted, how do I ask Auth to add a filter
I recommend you to define another middleware that detects if user is admin instead of modifying the auth. Now add this another middleware to your routes that only admins can access.
Add several middleware to route like this
Route::group(['middleware' => ['auth','admin']], function() {
Middleware will look something like
public function handle($request, Closure $next) {
if (Auth::user()->role == "admin") {
return $next($request);
} else {
return redirect("/")->withMyerror("You are not authorized for this action");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With