简体   繁体   中英

Does Google login in Laravel Socialite contain user's phone number?

I implemented Google login using Socialite in Laravel. The example I used only contains the user's name, google_id, email and profile picture. Is there a way to get the phone number from the Google callback? Or does it even contain the number? Here's the function that handles the callback:

public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('google')->user();
    } catch (\Exception $e) {
        return redirect('/login');
    }
    // check if they're an existing user
    $existingUser = User::where('email', $user->email)->first();
    if($existingUser){
        // log them in
        auth()->login($existingUser, true);
    } else {
        // create a new user
        $newUser                  = new User;
        $newUser->name            = $user->name;
        $newUser->email           = $user->email;
        $newUser->google_id       = $user->id;
        $newUser->avatar          = $user->avatar;
        $newUser->avatar_original = $user->avatar_original;
        $newUser->save();
        auth()->login($newUser, true);
    }
    return redirect()->to('/');
}

that is not possible. You should add a middleware that verifies your users have a phone. This way, you are sure your users will provide a phone no matter how they registered.

It is not supported.

Looking at the provider , we can see it is using OpenID Connect . And so the phone_number claim should be available. But as per documentation , Google has it's own supported claims:

"claims_supported": [
    "aud",
    "email",
    "email_verified",
    "exp",
    "family_name",
    "given_name",
    "iat",
    "iss",
    "locale",
    "name",
    "picture",
    "sub"
],

These claims are later mapped using:

protected function mapUserToObject(array $user)
{
    return (new User())->setRaw($user)->map([
        'id'       => $user['sub'],
        'nickname' => Arr::get($user, 'name'),
        'name'     => $user['name'],
        'email'    => $user['email'],
        'avatar'   => $user['picture'],
    ]);
}

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