Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign in With Apple does not contain a DisplayName #10306

Closed
Lyokone opened this issue Oct 5, 2022 · 5 comments
Closed

Sign in With Apple does not contain a DisplayName #10306

Lyokone opened this issue Oct 5, 2022 · 5 comments

Comments

@Lyokone
Copy link

Lyokone commented Oct 5, 2022

Description

When using Sign In With Apple, following this guide, I don't have access to the display name which is always null even if I properly added the scope for accessing the name.

I properly verified that this is the first time this user is Sign In with Apple.

First reported here: firebase/flutterfire#9662

Reproducing the issue

- (void)authorizationController:(ASAuthorizationController *)controller
    didCompleteWithAuthorization:(ASAuthorization *)authorization
    API_AVAILABLE(macos(10.15), ios(13.0)) {
  if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
    ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
    NSString *rawNonce = self.currentNonce;
    NSAssert(rawNonce != nil,
             @"Invalid state: A login callback was received, but no login request was sent.");

    if (appleIDCredential.identityToken == nil) {
      NSLog(@"Unable to fetch identity token.");
      return;
    }

    NSString *idToken = [[NSString alloc] initWithData:appleIDCredential.identityToken
                                              encoding:NSUTF8StringEncoding];
    if (idToken == nil) {
      NSLog(@"Unable to serialize id token from data: %@", appleIDCredential.identityToken);
    }

    // Initialize a Firebase credential.
    FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
                                                                        IDToken:idToken
                                                                       rawNonce:rawNonce];

    if (self.linkWithAppleUser != nil) {
      [self.linkWithAppleUser linkWithCredential:credential
                                      completion:^(FIRAuthDataResult *authResult, NSError *error) {
                                        handleSignInWithApple(self, authResult, error);
                                      }];

    } else {
      [FIRAuth.auth signInWithCredential:credential
                              completion:^(FIRAuthDataResult *_Nullable authResult,
                                           NSError *_Nullable error) {
                                handleSignInWithApple(self, authResult, error);
                              }];
    }
  }
}

The authorization object does contain the family name and the given name, but the FIRAuthDataResult *_Nullable authResult has an empty displayName

Firebase SDK Version

9.6.0

Xcode Version

Version 14.0.1

Installation Method

CocoaPods

Firebase Product(s)

Authentication

Targeted Platforms

iOS

Relevant Log Output

No response

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
Replace this line with the contents of your Package.resolved.

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
Replace this line with the contents of your Podfile.lock!
@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@rizafran
Copy link
Contributor

rizafran commented Oct 5, 2022

Hi @Lyokone, it seems that your issue is related to #4393.

@Lyokone
Copy link
Author

Lyokone commented Oct 6, 2022

Thanks, this PR: #10068 should solve the issue

@peterfriese
Copy link
Contributor

A video explaining how to update the display name is going live tomorrow. In the meantime, check out this code snippet (source: https://bit.ly/SiwA-updateDisplayName):

func updateDisplayName(for user: User, with appleIDCredential: ASAuthorizationAppleIDCredential, force: Bool = false) async {
    if let currentDisplayName = Auth.auth().currentUser?.displayName, !currentDisplayName.isEmpty {
      // current user is non-empty, don't overwrite it
    }
    else {
      let changeRequest = user.createProfileChangeRequest()
      changeRequest.displayName = appleIDCredential.displayName()
      do {
        try await changeRequest.commitChanges()
        self.displayName = Auth.auth().currentUser?.displayName ?? ""
      }
      catch {
        print("Unable to update the user's displayname: \(error.localizedDescription)")
        errorMessage = error.localizedDescription
      }
    }
  }

@paulb777
Copy link
Member

paulb777 commented Jan 6, 2024

Based on the comments, this seems to be fixed.

@paulb777 paulb777 closed this as completed Jan 6, 2024
@firebase firebase locked and limited conversation to collaborators Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants