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

reaz's avatar
Level 5

Dont show login page page, if user is logged in

My problem is somewhat similar to this problem: https://laracasts.com/discuss/channels/laravel/how-to-prevent-showing-login-page-after-user-logged-and-hit-browser-back-button

In my case , it is a closed system, so main page is the login page. Now i want to prevent a logged in user to visit this page. In laravel default auth system, once you are logged in you cannot visit /login route. How do i replicate this behavior for main page. So if a logged in user visit main page (fx mydomain.com) they will redirected to their homepage. Thanks in advance

0 likes
7 replies
reaz's avatar
Level 5

@snapey , My problem is not really the back button. Since multiple user will be logging in form the the same device, So imagine user1 logged in, but did not log out. Then loaded some other website. Now second user comes in enters the 'mydomain.com' in the address. This will show the homepage(login page), user2 enter his login details, but after clicking login ends up in first user's homepage, which would be confusing to the user2. It would be great if second user enter the address , then without showing the login page(form), it ends up in first user's homepage. So he know someone else is logged in, so he will log out the first user.

This behavior already happens with the /login page in the laravel(5.8) default auth. I just want the same thing on the homepage because this is my login page. Thanks

Snapey's avatar

user2 enter his login details, but after clicking login ends up in first user's homepage,

There is no way this should be happening if the login controller is being called correctly

reaz's avatar
Level 5

This also seems odd to me, but in fact this is happening. I am using default auth scaffolding of version 5.8 . Only change i have made is to copy the form from login page , and moved it to welcome page. Then changed the unauthenticated function like this:

protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $request->expectsJson()
                    ? response()->json(['message' => $exception->getMessage()], 401)
                    : redirect()->guest(route('welcome'));
    }

For now to solve the problem stated in this question, i have added this bit in the web.php file:

Route::get('/', function () {
    if (Auth::check()) {
        return redirect()->route('home');
    }
    return view('welcome');
})->name('welcome');

Do you think this is right approach? @snapey

bibekgiri's avatar
Level 9

I came up with this solution in my route file i simply gave the controller to to the login page like this.

Route::get('/', 'Auth\LoginController@showLoginForm')->name('login');

And in fact it's still working now and haven't faced any issue up until now.

Please or to participate in this conversation.