Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Arslan's avatar

The page isn't redirecting properly

This is My Route For Backend

Route::group(['prefix' => 'admin', 'namespace' => 'Backend', 'middleware' => 'auth' ], function()
{
    // GET
    # Admin Dashboard
    Route::resource('/', 'WelcomeController');
        Route::resource('/dashboard', 'WelcomeController');

    # Auth Dashboard
    Route::controllers([
        'auth' => 'Auth\AuthController',
    ])
}); 

My problem is when i use Auth Middlewhere its shows me Error The page isn't redirecting properly

In Authenticate.php i edit this

public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('admin/auth/login');
            }
        }

        return $next($request);
    }

If i just remove middlewhere from route then my page works fine. How can i fix it

0 likes
3 replies
thomaskim's avatar

You need to remove your Auth controller routes outside of the group. You're applying the auth middleware on the login/registration routes. Those routes should be specifically for people who aren't authenticated. How can guests access the login route if you require them to be authenticated?

Arslan's avatar

@thomaskim yes i understands that but i am using name space and this controller is just for Admin panel . For froentend i make a custom controller also

thomaskim's avatar

@Arslan But why does that matter? Your code has a logic flaw to it because it's just redirecting to itself an infinite number of times.

  1. Someone tries to access some admin route.
  2. If he is not authenticated, he is taken to the admin login route.
  3. You applied the auth middleware on the admin login route so the user can't access it.
  4. The auth middleware takes him back to the admin login route.
  5. The admin login route has the auth middleware on it so the user can't access it.
  6. The cycle repeats over and over.

If you want to create a separate login route for admins, you can do that, but you can't apply the auth middleware on it. At the least, guests shouldn't be redirected to the admin login route. They should be redirected to the "regular" login route.

1 like

Please or to participate in this conversation.