How to get additional parameter from Google OAuth?

Viewed 842

I use Laravel 5.8, PHP 7.3, MongoDB.

My app could have anonymous users, who could already have their ID in database. They also could have some data, that is connected to user_id.

My goal is to make registration for anonymous user keeping their data connected to them.

I remade standard Laravel registration for anonymous users, it wasn't a problem.

Now i started to make login with social networks. I made Google login firstly. The login and registration works perfectly for new users. But i can't even imagine how to make Google login to work with anonymous users without passing on the additional parameter to Google.

I have already tried:

  1. Use method with() to pass optional parameter to provider (Google in my case), but there is no information about how to get this additional parameter in callback request from provider. So i decided that this parameter is needed just for some validation for some providers, but i'm not sure.

  2. I found that your app could add dynamical parameters to the GitHub callback URL, but it doesn't work with Google.

My code is pretty usual for this task. I have used the post "From Google+ API to Google Sign In with Laravel Socialite" on laraveldaily.com.

Here is routes:

Route::get('redirect/{driver}', 
'Auth\LoginController@redirectToProvider')
    ->name('login.provider')
    ->where('driver', implode('|', config('auth.socialite.drivers')));

Route::get('{driver}/callback', 
'Auth\LoginController@handleProviderCallback')
    ->name('login.callback')
    ->where('driver', implode('|', config('auth.socialite.drivers')));

Here is function for redirecting user to provider:

public function redirectToProvider($driver)
    {
        return Socialite::driver($driver)->redirect();
    }

And here is the function for handling callback from the provider:

public function handleProviderCallback($driver)
    {
        try {
            $user = Socialite::driver($driver)
                ->stateless()
                ->user();
        } catch (\Exception $e) {
            return redirect()->route('register');
        }

        $existingUser = User::where('email', $user->getEmail())->first();

        if ($existingUser) {
            auth()->login($existingUser, true);
        } else {
            $newUser                    = new User;
            $newUser->provider_name     = $driver;
            $newUser->provider_id       = $user->getId();
            $newUser->name              = $user->getName();
            $newUser->email             = $user->getEmail();
            $newUser->email_verified_at = now();
            $newUser->avatar            = $user->getAvatar();
            $newUser->save();

            auth()->login($newUser, true);
        }

        return redirect($this->redirectPath());
    }

So i would be really happy if someone could help me to understand how i could pass parameters to providers and get it back from providers.

If my English or problem description is not pretty understandable, please, write comments, i will explain it by another way.

Thanks in advance for your ideas!

0 Answers
Related