Ibrahim
Ibrahim

Reputation: 65

This page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

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

Answers (2)

A.A Noman
A.A Noman

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

Giacomo M
Giacomo M

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

Related Questions