Laravel 5.2 Auth::login not persisting logged user

被刻印的时光 ゝ 提交于 2019-12-10 23:26:50

问题


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

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.

Though I am using the web middleware

here is my code in 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');    
});

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

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

but after redirecting it gives me null

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


回答1:


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.



来源:https://stackoverflow.com/questions/35310628/laravel-5-2-authlogin-not-persisting-logged-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!