Laravel sessions are destroyed after login from Google

224 Views Asked by At

I am using Laravel Shopping cart and Laravel Socialite for login, login works fine but when the user logs in it deletes his Cart and all other sesshions on the page, do you know how to fix it ? Thanks

The reason I need this is that the user has the option to complete the order logged in or as a guest, to log in they have the option of using the login form or using the "Login with Google" button When the user logs in using the login form the** Cart stays**, but only when he uses the google login so it deletes it

LOGIN CONTROLLER `

 public function __construct()
    {
        $this->middleware('guest')->except('logout');  
    }


          
     // Google login
                public function redirectToGoogle()
                { 
                    return Socialite::driver('google')->stateless()->redirect();
                    
                }
            
                // Google callback
                public function handleGoogleCallback()
                {
                 
                   
                   
                    $user = Socialite::driver('google')->stateless()->user();
                    
                    
                    $this->_registerOrLoginUser($user);
                   
                    // Return home after login
                    return redirect()->route('checkout.index')
                    ->with('toast_success',  'Byli jste úspěšně přihlášeni :)');
                    
                }
            
             
            
             
     protected function _registerOrLoginUser($data)
     {
       
      
            $user = User::where('email', '=', $data->email)->first();
            if (!$user) {
             $user = new User();
             $user->name = $data->name;
             $user->email = $data->email;
             $user->provider_id = $data->id;
             $user->avatar = $data->avatar;
             $user->save();
         }
        
         Auth::login($user);
        
         
     }
    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        session()->put('previousUrl', url()->previous());

        return view('auth.login');
    }

`

Otherwise if I try to put Cart into collection (collect(session()->pull('cart')) before the login is processed, it deletes that too, I tried session regenerate and nothing works either. So far it seems to me that after login the page gets a completely "new indentity" ¯_(ツ)_/¯

1

There are 1 best solutions below

1
Wenzz On

It may have something to do with the fact the session ID gets regenerated when you call the function $this->_registerOrLoginUser($user);

You could try storing the cart session in a collection or array before that function is called, and then afterwards try to repopulate the session using that collection/array after $this->_registerOrLoginUser($user);