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

lcf8381595's avatar

laravel 5.5 auth middleware, redirect to login page, can show some message?

when use auth middleware, it redirect to login page if user did not login, how can i show some message like "you have to login to access this page" when redirect to login page?

thanks.

0 likes
4 replies
Drfraker's avatar
Drfraker
Best Answer
Level 28

In the exception handler which is found in App\Exceptions\Handler you can add a flash message when AuthenticationException is thrown.

public function render(Exception $exception)
{
    if ($exception instanceof AuthenticationException) {
        Session::flash('login-error', 'You must be logged in to do that...');
    }

    return parent::render($exception);
}

Then in your login view, check for that error in the session and display a message when it is present.

2 likes
aardalich's avatar

Could check for the url.intended key in the session that laravel auth puts the url to redirect after the successful auth

Minnow990's avatar

@Drfraker I've been searching for help with this for ages, and I never ever thought to look in the exceptions handler. Now to move on with my life...

2 likes

Please or to participate in this conversation.