[英]laravel Auth::user() returns null after authentication
I am getting Auth::user() null in a controller.我在控制器中得到 Auth::user() null。 After logging in with credentials i normally get the authenticated user data but in one of my projects i am not getting the authenticated user data.使用凭据登录后,我通常会获得经过身份验证的用户数据,但在我的一个项目中,我没有获得经过身份验证的用户数据。
For explanation i am posting the code snippets.为了解释,我发布了代码片段。 In NewLoginController i try to attempt the login using credentials and it is successfully logging in when i try with credentials and i can see the admin view, but when i try to go to homepage using /homepage route i get Auth::check() false and not be able to maintain the flow of the application, while in my other projects it is the same flow i have used.在 NewLoginController 中,我尝试使用凭据尝试登录,当我尝试使用凭据时它成功登录并且我可以看到管理员视图,但是当我尝试使用 /homepage 路由转到主页时,我得到 Auth::check() false并且无法维护应用程序的流程,而在我的其他项目中,它与我使用的流程相同。
Can any one guide me resolving this issue ?任何人都可以指导我解决这个问题吗?
class NewLoginController extends Controller
{
public function attemptLoginApi()
{
if (Auth::attempt(['username' => request('username'), 'password' => request('password')])) {
$userType = Auth::user()->user_type;
return view('admin');
} else {
toastr()->error('Invalid Credentials', 'Error');
return back();
}
}
}
class HomePageController extends Controller
{
public function getUserHomepage()
{
if (Auth::check()) {
return view('admin');
} else {
return view('home');
}
}
}
web.php网页.php
Route::get('/homepage', 'HomePageController@getUserHomepage');
Route::post('/loginApi', 'NewLoginController@attemptLoginApi')->name("loginApi");
I think you have forgotten to use Auth
middleware.我认为您忘记使用Auth
中间件。 Put your homepage
route in a route group that uses Auth
middleware.将您的homepage
路由放在使用Auth
中间件的路由组中。 See the documentation .请参阅文档。
Route::group(['middleware'=>'auth'],function () {
Route::get('/homepage', 'HomePageController@getUserHomepage');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.