I am building a login/register module using Laravel 5.1
I have defined the following routes
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', ['middleware' => 'auth', function () {
return view('home');
}]);
I have also created a view for welcome and home. When I enter my credentials and login, I get redirected to the home page.
How do I make sure that once I am authenticated and I try to access the '/' route, I get redirected to the home page.
Currently once I am logged in and reach the home page and I type http://localhost:8000/ I get back to the login page even though I am still logged in. How can I prevent this?
Check If user is authenticated and if user is authenticated then redirect to home
Route::get('/', function () {
if(Auth::check()){return Redirect::to('home');}
return view('welcome');
});
I would approach this the other way around. I would always go to home, and if they are not authenticated, redirect them to the login page.
In your controller:
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest:user', ['except' => ['logout']]);
}
(with user is the auth logging in)
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.