简体   繁体   English

如果在Laravel中通过身份验证,则将用户从登录页面重定向到主页

[英]Redirecting User from Login Page to Home Page if Authenticated in Laravel

I am building a login/register module using Laravel 5.1 我正在使用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? 当前,一旦我登录并进入主页并键入http:// localhost:8000 /,即使我仍在登录,我也返回登录页面。如何防止这种情况发生?

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) (用户是登录的身份验证)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel,如果用户未通过身份验证,则重定向到特定的登录页面 - Laravel, redirect to specific login page if user is not authenticated 当用户未通过身份验证时,如何让Laravel发送登录表单,而不是重定向到登录页面? - How do I have Laravel send a login form when a user isn't authenticated, rather than redirecting to a login page? 允许经过身份验证的用户平等访问 Laravel 5.8 默认登录页面 - Allow authenticated user to equally access laravel 5.8 default login page 将用户重定向到“登录OpenID”页面 - Redirecting user to Login OpenID page Laravel 用户基于角色登录到不同的主页 - Laravel user login based on roles to different home page 将经过身份验证的用户重定向到注册页面以外的其他页面 - Redirecting the authenticated user to a different page other than the signup page 使用他们的用户角色从同一登录页面将用户重定向到仪表板 - Redirecting user to dashboard with their user role from same login page 从登录页面重定向的问题 - Issues redirecting from login page 无法从数据库 symfony 4 中验证用户重定向到同一登录页面 - not able to verify user from database symfony 4 redirecting to same login page 路由重定向到登录页面Laravel 5.2 - Routes Redirecting to the login page Laravel 5.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM