Laravel 5.2 Auth :: login无法持久保存登录的用户 [英] Laravel 5.2 Auth::login not persisting logged user

查看:124
本文介绍了Laravel 5.2 Auth :: login无法持久保存登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个身份验证系统,该系统也可以使用Socialite进行社交登录.

I have an authentication system which has also social login using Socialite.

问题是当我从社交媒体成功通过身份验证后使用Auth::login()登录用户时,会话在重定向后不会持续.

The problem is when I use Auth::login() to log in the user when successfully authenticated from social media, the session doesn't persist after redirecting.

尽管我正在使用web中间件

这是我在routes.php

Route::group(['namespace' => 'Frontend', 'middleware' => 'web'], function () {
    Route::get('/', 'PagesController@index');
    Route::get('login/{provider?}', 'AuthController@getLogin');
    Route::post('login', 'AuthController@postLogin');
    Route::get('login/callback/google', 'AuthController@getGoogleCallback');
    Route::get('login/callback/facebook', 'AuthController@getFacebookCallback');    
});

例如,当用户在Facebook中成功通过身份验证时,我会在控制器中使用它

and when the user is successfully authenticated in Facebook for example, I use this in my controller

Auth::login($user);
return redirect()->to('/');

但是重定向后,它给了我null

but after redirecting it gives me null

我也将User.php移到了app/Models/User.php,并在config/auth.php中进行了所有必要的更改.

I also moved my User.php to app/Models/User.php and made all the necessary changes in config/auth.php.

推荐答案

您缺少主要路线的中间件= auth.

You're missing the middleware = auth for your main route.

Route::group(['namespace' => 'Frontend', 'middleware' => ['web','auth'], function () {
     Route::get('/', 'PagesController@index');    
});

基本上,您的主路由应该具有身份验证中间件,但其他路由则没有,因为访客用户需要能够访问登录页面.

Basically your main route should have an auth middleware but not other routes as the guest users will need to be able to access to login page.

这篇关于Laravel 5.2 Auth :: login无法持久保存登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆