2

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);
    }
Dilpreet Singh
  • 51
  • 1
  • 10
  • 1
    Welcome to StackOverflow. Please share your code and the details of the error you are getting. This will make it easier for other user to provide some help. Thanks! – LobsterBaz Apr 16 '20 at 20:59
  • @LobsterBaz, Thanks for your warm welcome. I updated the question with the code. Could you please have a look on that now :) – Dilpreet Singh Apr 17 '20 at 06:32
  • Hi, I don't use Socialite but it looks like your error comes from this line: `$user = Socialite::driver('google')->user();`. Would the following answer be helpful? https://stackoverflow.com/questions/30660847 It looks similar to your error. – LobsterBaz Apr 17 '20 at 23:15
  • Thanks @LobsterBaz, I already checked out the above link but nothing works for me. When i tried the `stateless()` it is giving me the Bad Request error also. – Dilpreet Singh Apr 18 '20 at 15:21
  • @LobsterBaz Thanks a lot for you kind help, The shared link is 50% useful in solving the issue – Dilpreet Singh Apr 19 '20 at 10:07

1 Answers1

3

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 */
    }
Dilpreet Singh
  • 51
  • 1
  • 10