简体   繁体   中英

Laravel 9 - How to prevent showing login page after user is logged-in and hit browser back button

How can I ensure that as soon as the user is logged-in in Laravel-9 he can no longer go to the login page via browser back button?
I searched the inte.net for solutions. I have read in several places that it is not possible or that I have to use Javascript.
Just to be sure, I have decided to post my question here and I hope you can help me.
Is there any way to do this? If the solution is with javascript, how can I solve that with javascript?

Thanks

if you open this Middleware /app/Http/Middleware/RedirectIfAuthenticated.php in your project, can see the handle function with this condition:

       ...

  if (Auth::guard($guard)->check()) {
    return redirect(RouteServiceProvider::HOME);
  }
       ...

It means that after login to the site if users try to go to the login page, the browser redirects the page to the HOME address. So users cannot access the login page after logging into your site.
you can edit HOME from the /app/Providers/RouteServiceProvider.php file.

This page does list a number of ways you could try to disable the back button via javascript, but none are guaranteed.

By default Laravel 9 has the RedirectIfAuthenticated middleware under App\Http\Middleware which checks if the user is logged in Auth::guard($guard)->check() and if they are they are taken to the /dashboard url otherwise they are not. The Middleware is registered as 'guest' in the $routeMiddleware array inside Kernel.php , this means that you can apply guest middleware to all routes that you do not need be accessed by logged in users.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM