问题
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