Laravel 5 redirect to home page if attempting to go to login screen

23,667

Solution 1

I did this in my router, although I'm not sure if it's the best solution:

Route::get('/', function () {
    if(Auth::check()) {
        return redirect('/dashboard');
    } else {
        return view('auth.login');
    }
});

Solution 2

You should use middleware for this.

Laravel 5 has already a 'guest' middleware out of the box you can use, so just doing the following should be enough:

Route::get('/', ['middleware' =>'guest', function(){
  return view('auth.login');
}]);

Then in the middleware file App\Http\Middleware\RedirectIfAuthenticated you can specify where the user is redirected to.

The default is /home.

Solution 3

In my case I had to delete the cookies in my browser in order to fix the /login redirect.

Solution 4

When a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle. You can customize the post-authentication redirect location by defining a redirectPath property on the AuthController:

in your AuthController change the redirectPath property

protected $redirectPath = '/dashboard';

http://laravel.com/docs/5.1/authentication#Authenticating

Share:
23,667
imperium2335
Author by

imperium2335

PHP, JS, MySQL coder and retired 3D modeler.

Updated on June 16, 2020

Comments

  • imperium2335
    imperium2335 almost 4 years

    I have a login screen which is the base url (www.mysite.com/).

    When a user logs in they are redirected to their home page (/home).

    But the can still get back to the login page if they go to the root.

    How do I make it so logged in users are sent back to their home page if they are logged in (if they are not, of course they see the login page)?