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)?
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');
}
});
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With