laravelgoogle-signinlaravel-socialite

Laravel Socialite Google Signin not working with the profile scope


I am facing the issue for the Social login implementation in the Laravel project using the Socialite package.

When try to use the Google Sign in it is giving me null error string and no exception message but when i try to remove the profile scope then it logged me in but there is no profile data of the logged user.

I search for the results but got nothing even on he Socialite Github

This is the code from the Socialite Package in my Controller


/**
     * Obtain the user information from Google.
     *
     * @return \Illuminate\Http\Response
     */
    public function googleLogin()
    {
        try {
            // Not able to get the user data
            $user = Socialite::driver('google')->user();

        } catch (\Exception $e) {
            return redirect('/login')->with('error','Try after some time');
        }
        $existingUser = User::where('social_id', $user->id)->first();

                if (!empty($existingUser)) {

                    $getType = UserType::where('user_id',$existingUser['id'])->get()->toArray();
                    $count = count($getType);
                        if ($count == 2) {
                            return view('Front.types')->with('getTypes',$getType);
                        }elseif($count == 1){

                            if ($getType[0]['user_type'] == "consumer") {
                                \Auth::loginUsingId($existingUser['id']);
                                return redirect()->intended('/');

                            }
                            if ($getType[0]['user_type'] == "bizzowner") {
                                \Auth::loginUsingId($existingUser['id']);

                                return redirect('business/dashboard');
                            }
                        }

                        // if(!session()->has('url.intended'))
                        // {
                        //     session(['url.intended' => url()->previous()]);
                        // }

                }

I am always getting this error message on user display

Try after some time

Got this Exception message

Laravel\Socialite\Two\InvalidStateException {#616 ▼
  #message: ""
  #code: 0
  #file: "/home/greenalleymart/public_html/vendor/laravel/socialite/src/Two/AbstractProvider.php"
  #line: 210
  trace: {▼
    /home/greenalleymart/public_html/vendor/laravel/socialite/src/Two/AbstractProvider.php:210 {▶}
    /home/greenalleymart/public_html/app/Http/Controllers/Front/UserController.php:367 {▼
      App\Http\Controllers\Front\UserController->googleLogin() …
      › try {
      ›     $user = Socialite::driver('google')->user();
      ›     dd($user);
    }

Solution

  • After the lot of search and doing different stuff, finally found the solution.

    We have to initiate the Google OAUth2 stateless()

    Working Code

        public function redirectToGoogle(){
            return Socialite::driver('google')
                ->stateless()
                ->redirect();
        }
    
    
    
    
        /**
         * Obtain the user information from Google.
         *
         * @return \Illuminate\Http\Response
         */
            public function googleLogin()
        {
            try {
                $user = Socialite::driver('google')->stateless()->user();
                // dd($user);
            } catch (\Exception $e) {
                return redirect('/login')->with('error','Try after some time');
            }
                $existingUser = User::where('social_id', $user->id)->first();
    
            /** Your code */
        }