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

mbpp's avatar
Level 3

Default redirect login page if not authenticate in 5.4

I created a new Middleware called "MustBeAdmin", and in my function depending of the account_type_id i give permission or not, but i notice for example when i access a page without being authenticate im redirect to "/login" route, but since my default redirect cant be this route, i need to change it, but i cant find where i change this default route. Im using Laravel 5.4.

My Middleware is:

public function handle($request, Closure $next)
    {
        if(auth()->check() && auth()->user()->account_type_id == 1){
            return $next($request);
        }

        return redirect('/admin');
    }
0 likes
7 replies
cviv's avatar

App\Http\Controllers\Auth\LoginController.php after logging user

App\Http\Middleware\RedirectIfAuthenticated.php if logged

Use route if you have different login route

1 like
mbpp's avatar
Level 3

But this doesnt answer my question. Where is located the path that says wich route your redirect after your tried to access a authenticated page?

sherwinmdev's avatar

@mbpp it should work. where do you have the middleware in kernel.php? it sounds like there's a middleware running before your MustBeAdmin. probably a auth middleware is running which is causing the redirect to login to happen.

mbpp's avatar
Level 3

@w1n78 i already tried changing its order with the authenticate and is giving the same issue.

ollyc1804's avatar
Level 6

@mbpp the path is defined in app/Exceptions/Handler.php

/**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        return redirect()->guest('elsewhere');
    }

change the 'elsewhere' to the url of the login page or where you want unauthenticated users to be redirected to.

5 likes

Please or to participate in this conversation.