Search code examples
phplaravellaravel-5laravel-7laravel-socialite

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('/');
}

Solution

  • 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.