简体   繁体   中英

Laravel 5.2 Auth::login not persisting logged user

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM