Reputation: 65
hello I've been struggling with this error since yesterday, I need some help here is my middleware and here my route. This page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS.
public function handle($request, Closure $next)
{
if (Auth::check()) {
$user = Auth::user();
if ($user->hasAnyRole('school')) {
return $next($request);
}
} else {
return redirect('login');
}
}
Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about')->name('about');
Auth::routes();
Route::get('/school', 'HomeController@index')->name('school')->middleware('school');
It's even impossible to access the landing page. Thanks in advance!
Upvotes: 0
Views: 3925
Reputation: 5270
You have to use like this
public function __construct(){
public function handle($request, Closure $next)
if (Auth::check()) {
$user = Auth::user();
if ($user->hasAnyRole('school')) {
return $next($request);
}
else{
// Another redirect
}
} else {
return redirect()->route('login');
}
});
}
Upvotes: 0
Reputation: 4723
You need to redirect the user again in the else of hasAnyRole
function:
public function handle($request, Closure $next)
{
if (Auth::check()) {
$user = Auth::user();
if ($user->hasAnyRole('school')) {
return $next($request);
} else {
// HERE
return redirect('login');
}
} else {
return redirect('login');
}
}
Upvotes: 0