Search code examples
ioslaraveleloquentlaravel-socialite

Laravel Socialite Apple Sign In no user info


I add in my Laravel API on my login endpoint ability for user to sign-in with apple also when I try, everything from the config looks good also in my api I receive this:

    (
    [id] => 001519.7eab28c498c946828053eXXXXXXXX.XXXX
    [nickname] => 
    [name] => 
    [email] => 
    [avatar] => 
    [user] => Array
        (
            [iss] => https://appleid.apple.com
            [aud] => com.muze.mob
            [exp] => 1709642029
            [iat] => 1709555629
            [sub] => XXXXX.XXXXXXXXXXXXXXXXXXXX.XXXX
            [nonce] => XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            [c_hash] => XXXXXXXXXXXX
            [auth_time] => 1709555629
            [nonce_supported] => 1
        )

    [attributes] => Array
        (
            [id] => XXXXX.XXXXXXXXXXXXXXXXXXXXX.XXXX
            [name] => 
            [email] => 
        )

    [token] => eyJraWQiOiJCaDZIN3JIVm1iIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLm11emUubW9iIiwiZXhwIjoxNzA5NjQyMDI5LCJpYXQiOjE3MDk1NTU2MjksInN1YiI6IjAwMTUxOS43ZWFiMjhjNDk4Yzk0NjgyODA1M2VlYTIxYTM2ZmZjOC4xMTU5Iiwibm9uY2UiOiI1ZDVjMGRiMjE2NGU4MzZiM2YzXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    [refreshToken] => 
    [expiresIn] => 
    [approvedScopes] => 
    [accessTokenResponseBody] => 
)

As we can see the auth looks good also it miss user email .. Also I have an id but I don't understand why I can't use user mail ..

I use userFromToken from socialite with apple provider

$oauthUser = Socialite::driver($request->get('login_type'))->stateless()->userFromToken($request->get('token'));                
$username = $oauthUser->getName() && !User::where('username', $oauthUser->getName())->exists() && !User::where('pseudo', $oauthUser->getName())->exists() ? $oauthUser->getName() : $this->generateUniqueUsername();
                

                $user = User::firstOrCreate(
                    ['email' => $oauthUser->getEmail()],
                    [
                        'username' => str_replace(' ', '', $username),
                        'pseudo' => str_replace(' ', '', $username),
                        'first_name' => $oauthUser->user['given_name'] ?? null,
                        'last_name' => $oauthUser->user['family_name'] ?? null,
                        'avatar' => $oauthUser->getAvatar(),
                        'email_verified_at' => now(),
                        'password' => Hash::make(substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 30))
                    ]
                );

What is missing ? Even if user doesn't want to share his mail apple would generate one.

How to resolve this ?

Thanks


Solution

  • It is normal for only the user's information to be sent in response during their initial registration. For subsequent connections with the same Apple account, the user's information is not shared, and only a user identifier is returned in the response, as in your case. It is recommended to securely cache the initial response containing user information (email, first name, last name) until you can confirm that an account has been successfully created on your server.

    In my case, to receive information during an initial connection, I had to access the settings of my iPhone to delete the application linked to my Apple ID. If you're using an iPhone, you can access Settings, click on your name, then click on "Sign-In & Security" and then on "Sign in with Apple". You'll see a list of applications that you use with Sign in with Apple. You can then delete the application (sometimes, it may be necessary to persist in deleting it).

    Screenshot for reference

    Try logging in again, and you should receive the user information because it's that crucial initial connection.

    Here are the different links: