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

Add Sign in with Apple Display Name API and unit test #10068

Merged
merged 25 commits into from
Mar 9, 2023

Conversation

aiwenisevan
Copy link
Contributor

@aiwenisevan aiwenisevan commented Aug 4, 2022

Discussion

Testing

  • Unit testing Passed for both FIROAuthProviderTest and FIRAuthTest

API Changes

@aiwenisevan aiwenisevan requested a review from rosalyntan August 4, 2022 00:25
Copy link
Member

@rosalyntan rosalyntan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One initial comment: Please remove the files under AuthSample.xcodeproj and xcshareddata/xcschemes, as well as Application.plist from the PR -- these files should only be updated locally.

I'll take a more in-depth look at this later today!

.ruby-version Outdated Show resolved Hide resolved
FirebaseAuth/Tests/Sample/Sample/GoogleAuthProvider.m Outdated Show resolved Hide resolved
FirebaseAuth/Tests/Unit/FIRAuthTests.m Outdated Show resolved Hide resolved
FirebaseAuth/Tests/Unit/FIRAuthTests.m Outdated Show resolved Hide resolved
FirebaseAuth/Tests/Unit/FIRAuthTests.m Outdated Show resolved Hide resolved
@google-oss-bot
Copy link

google-oss-bot commented Aug 12, 2022

@peterfriese peterfriese changed the title Add api addtion for SIWA and add Function for Unit test Aug 22, 2022
@peterfriese peterfriese changed the title Add _Sign in with Apple Display Name API_ and unit test Aug 22, 2022
@rosalyntan rosalyntan marked this pull request as ready for review February 21, 2023 23:44
@rosalyntan rosalyntan requested a review from renkelvin February 21, 2023 23:44
Copy link
Contributor

@renkelvin renkelvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

FirebaseAuth/Tests/Unit/FIRAuthTests.m Show resolved Hide resolved
@byaruhaf
Copy link

So does this mean the display name is auto-updated so there is no need to update display name with appleIDCredential?

@peterfriese
Copy link
Contributor

So does this mean the display name is auto-updated so there is no need to update display name with appleIDCredential?

After receiving ASAuthorization from Apple, call appleCredential(withIDToken: rawNonce: fullName:) to create the credential, and then sign in.

Here is the relevant code snippet from the documentation:

@available(iOS 13.0, *)
extension MainViewController: ASAuthorizationControllerDelegate {

  func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
      guard let nonce = currentNonce else {
        fatalError("Invalid state: A login callback was received, but no login request was sent.")
      }
      guard let appleIDToken = appleIDCredential.identityToken else {
        print("Unable to fetch identity token")
        return
      }
      guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
        print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
        return
      }
      // Initialize a Firebase credential, including the user's full name.
      let credential = OAuthProvider.appleCredential(withIDToken: idTokenString,
                                                        rawNonce: nonce,
                                                        fullName: appleIDCredential.fullName)
      // Sign in with Firebase.
      Auth.auth().signIn(with: credential) { (authResult, error) in
        if error {
          // Error. If error.code == .MissingOrInvalidNonce, make sure
          // you're sending the SHA256-hashed nonce as a hex string with
          // your request to Apple.
          print(error.localizedDescription)
          return
        }
        // User is signed in to Firebase with Apple.
        // ...
      }
    }
  }

  func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
    // Handle error.
    print("Sign in with Apple errored: \(error)")
  }

}

As you can see, you no longer need to update the user's displayName in a separate operation, it's all done for you.

@byaruhaf
Copy link

This is Awesome Thanks Peter

@LilaQ
Copy link

LilaQ commented Apr 3, 2023

@peterfriese I feel like I'm missing something. In the Auth.auth().addStateDidChangeListener the user still comes with a nil for a displayName for me

@peterfriese
Copy link
Contributor

peterfriese commented Apr 3, 2023

Hi @LilaQ, I just verified this works as intended with Firebase 10.7.0.

If you're on the latest version of the SDK, can you share an MVCE with us? You can also check out the code in this PR - it contains the updated call that updates the user's display name when signing in with Apple.

@jscottarmstrong
Copy link

@LilaQ are you using the local emulator? If I run my app connected to the local emulator for authentication, I don't see full name getting populated, but if I connect to the my live Firebase app, it is working as expected with 10.7.0.

@firebase firebase locked and limited conversation to collaborators Apr 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.