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 not passing through fullName #4393

Open
RobSwish opened this issue Nov 25, 2019 · 39 comments
Open

Sign in with Apple not passing through fullName #4393

RobSwish opened this issue Nov 25, 2019 · 39 comments

Comments

@RobSwish
Copy link

RobSwish commented Nov 25, 2019

  • Xcode version: Version 11.2.1 (11B53)
  • Firebase SDK version: 6.13.0
  • Firebase Component: Auth - Sign in with Apple

Problem

I have added "Sign in with Apple" to my app. It all works perfectly except it does not pass the name through when authenticating. I am asking for full name and email in my code.

request.requestedScopes = [.fullName, .email]

The Sign in with Apple UI does display a cross next to the name, I don't know if it is supposed to show a tick, I've tried tapping this and changing my name but it does not change to a tick.

I can confirm through debugging that the "displayName" property of the Firebase user is nil after creation.

IMG_0321

Relevant Code:

request.requestedScopes = [.fullName, .email]

@rizafran
Copy link
Contributor

rizafran commented Nov 26, 2019

Thanks for reporting, @RobSwish. I'll try to replicate this on my end and I'll let you know with any updates.

@rizafran
Copy link
Contributor

rizafran commented Nov 26, 2019

For the Sign in with Apple UI, it might be an intended behavior that the cross icon displays next to the name, and not changing to a tick. You may check out the same UI here.

@rizafran
Copy link
Contributor

rizafran commented Nov 27, 2019

Hi @RobSwish, I was able to replicate the issue you encountered, and this is an expected behavior. The fullName is not returned in Apple's ID token so that we can't use it as displayName directly. However, you may opt to get the fullName through "appleIDCredential.fullName" in didCompleteWithAuthorization delegate method, and update user's profile by yourselves.

Additionally, it is mentioned here that Apple only shares the address with apps the first time a user signs in.

@paulb777
Copy link
Member

paulb777 commented Nov 27, 2019

Going to close this, since there's no obvious actions for us. Feel free to follow-up if you have more questions.

@jlubeck
Copy link

jlubeck commented Dec 14, 2019

The documentation here says that:

Usually, Firebase stores the display name the first time a user signs in with Apple, which you can get with Auth.auth().currentUser.displayName. However, if you previously used Apple to sign a user in to the app without using Firebase, Apple will not provide Firebase with the user's display name.

But this is not the case.

I was able to update my user based on @rizafran 's comment (thanks!) but that is not what the doc says. So either changing the docs or making the code do what the docs say would be nice (this last option would be preferred in my opinion!)

@morganchen12 morganchen12 reopened this Dec 16, 2019
@morganchen12
Copy link
Contributor

morganchen12 commented Dec 16, 2019

@renkelvin based on my understanding we should update the documentation here, not the SDK behavior. Is this correct?

@RobSwish
Copy link
Author

RobSwish commented Dec 18, 2019

Thanks a lot! You got back to me so quickly I didn't notice, sorry!

I do think that the default behaviour should be for Firebase to save the name if it can as this is what it does for the other sign in methods such as Facebook and Google.

But it's good to know I can get the name through the other way you have mentioned.

@morganchen12
Copy link
Contributor

morganchen12 commented Dec 18, 2019

I talked to @renkelvin about this and he mentioned that the current behavior exists because Sign In with Apple's behavior is inconsistent with the other authentication providers (Sign In with Apple does not always return the user's name).

I don't think Firebase saving the name to the user by default is a good idea since it makes it easier to accidentally link the name to other identifiable information, which is against Apple's license agreement if the user has chosen to anonymize their sign in info.

Unless this proves to be a significant pain point during development, we're likely not going to change the Firebase SDK behavior, though we can add this as an opt-in automatic behavior in FirebaseUI (see firebase/FirebaseUI-iOS#815).

@ghost
Copy link

ghost commented Mar 5, 2020

@morganchen12 How is the fullName differs from the email address in this case?

If the app requested both fullName and Email request.requestedScopes = [.fullName, .email]
and the user provided this information, why does firebase store only email as part of FirebaseUser object ?

Below is the relevant part from the agreement:

If a user has chosen to anonymize their user data as part of Sign In with Apple, You agree not to attempt to link such anonymized data with information that directly identifies the individual and that is obtained outside of Sign In with Apple without first obtaining user consent.

`

@brkeyal
Copy link

brkeyal commented Mar 11, 2020

I've re-created the scenario where an Apple user is new and signs in for my app for the first time, and still, Firebase's user (FIRUser) held a null displayName.
Definitely Apple provided the givenName and familyName on that login (on the appleID Credential), but Firebase just doesn't fetch it. Seems like a bug with Firebase.

@SwapnanilDhol
Copy link

SwapnanilDhol commented Mar 11, 2020

I've re-created the scenario where an Apple user is new and signs in for my app for the first time, and still, Firebase's user (FIRUser) held a null displayName.
Definitely Apple provided the givenName and familyName on that login (on the appleID Credential), but Firebase just doesn't fetch it. Seems like a bug with Firebase.

I have the same problem. I removed my app from the list of apps I use Apple Auth with but while signing in I still don't get back the name of the user.

@morganchen12
Copy link
Contributor

morganchen12 commented Mar 11, 2020

Thanks all, I'll take another look at this issue.

@ParkingPal
Copy link

ParkingPal commented Mar 30, 2020

Hello, do we have an update on this issue? I'm an experiencing it, as well.

@kolbasan
Copy link

kolbasan commented Apr 3, 2020

Hi, I am also having this issue. Is this being worked on?

@ParkingPal
Copy link

ParkingPal commented Apr 3, 2020

I'm not sure, but I was able to find a work around that appears to work. The name is available only on the first time authenticating with Apple. In the authorizationController method that is in the docs, take the variable appleIDCredential that is given, and grab both the first and last name. For me, I had variables that looked something like: firstName = appleIDCredential.fullName?.givenName

Once you get those names saved as variables, be sure you save them to the DB, as you will not be able to access them again. Hope this helps you out.

@Chrichton
Copy link

Chrichton commented Apr 6, 2020

I am experiencing the same problem.
But: When I run my app in the simulator, I get appleIDCredential.fullName each time, when I sign-in.
But when I am using a real device, I don't get it ever.

Question: What does "The name is available only on the first time authenticating with Apple" mean exactly?
Regards, Heiko

@morganchen12
Copy link
Contributor

morganchen12 commented Apr 6, 2020

Question: What does "The name is available only on the first time authenticating with Apple" mean exactly?

It means you'll get the name from Sign in with Apple whenever Apple feels like giving it to you. Sign in with Apple is hostile to systems that try to save PII by design. If you want to guarantee an alias for your user, prompt them to input a display name.

@JeffersonSchuler
Copy link

JeffersonSchuler commented Apr 10, 2020

I think the docs need to be updated to reflect the current behavior, or the behavior needs to be fixed. This issue has been open for quite awhile.

@Chrichton
Copy link

Chrichton commented Apr 10, 2020

I do now know, what the solution to the problem is.
It is explained brilliantly in Peter Friese's blog:
https://peterfriese.dev/replicating-reminder-swiftui-firebase-part3/

  1. When you authenticate for the first time, you get the name 100%. After that your app is linked to your iCloud account and you do not the name again.
    But you can unlink your app:
    As Peter Friese writes:
    "Unlinking your Apple ID from an app that uses Sign in with Apple
    Once you’ve signed in to your app, you cannot repeat the sign-in flow, which makes testing a bit of a challenge. To get back to the initial state, you need to disconnect your Apple ID from your app. Here is how:Go to https://appleid.apple.com and sign in using your Apple ID
    In the Security section, find Apps & Websites using Apple ID and click on Manage…
    You will see a pop-up dialog that shows you all apps that are connected to your Apple ID
    Managing apps using Apple ID.
    Click on the name of the app you want to disconnect
    The following dialog will tell you when you first started using your Apple ID with this application
    Click on Stop using Apple ID to disconnect this app from your Apple ID"

  2. Now you will get the name again.
    You retrieve it via:

if let fullName = appleIDCredential.fullName {
if let givenName = fullName.givenName, let familyName = fullName.familyName {

and via:

let changeRequest = user.createProfileChangeRequest() // (3)
changeRequest.displayName = displayName
changeRequest.commitChanges {

you tell Firebase to store the name into your profile.
Whenever you sign-in to Firebase, Auth.auth().currentUser will now have your fullname.

For detailed information, please look into Peter Friese's Blog.
Hopefully this helps and thanks to Peter Friese.

Best regards from Heiko

@JeffersonSchuler
Copy link

JeffersonSchuler commented Apr 10, 2020

I would like to point out that the behavior is different for Firebase on the web. Using that framework the display name is provided.

@jamiedaniel
Copy link

jamiedaniel commented Apr 20, 2020

I can’t get the above solution to work at all. Peter Friese has a great article, but I cannot get his solution to yield any positive results.

Anyone have a working example as of Swift 5.2?

@Chrichton
Copy link

Chrichton commented Apr 20, 2020

I can’t get the above solution to work at all. Peter Friese has a great article, but I cannot get his solution to yield any positive results.

Anyone have a working example as of Swift 5.2?

Hi Jamie.
I am using Swift 5.2.
The solution by Peter Friese works perfectly for me.

1.. Did you unlink the appleid?
2. Did
if let fullName = appleIDCredential.fullName {
if let givenName = fullName.givenName, let familyName = fullName.familyName {
Give you the fullName?

  1. Did
    changeRequest.commitChanges
    Succeed ?

Best regards from Heiko

@ParkingPal
Copy link

ParkingPal commented Apr 20, 2020

@jamiedaniel
Copy link

jamiedaniel commented Apr 20, 2020

I can’t get the above solution to work at all. Peter Friese has a great article, but I cannot get his solution to yield any positive results.
Anyone have a working example as of Swift 5.2?

Hi Jamie.
I am using Swift 5.2.
The solution by Peter Friese works perfectly for me.

1.. Did you unlink the appleid?
2. Did
if let fullName = appleIDCredential.fullName {
if let givenName = fullName.givenName, let familyName = fullName.familyName {
Give you the fullName?

  1. Did
    changeRequest.commitChanges
    Succeed ?

Best regards from Heiko

Yes I unlinked the appleid

I am not getting the display name

I’m not sure why but the app doesn’t register that I have logged in at all. Shouldn’t Firebase lost a new user in the Authentication tab?

@jamiedaniel
Copy link

jamiedaniel commented Apr 20, 2020

I currently have it retrieving the name from an apple user and writing it to the database. Haven't fully tested the part of the code that saves it as Auth.auth().currentUser.displayName fully, yet. But, I believe I have that working, as well. I have my set up almost precisely like it is in the documentation for Sign in with Apple for Firebase.

I’m trying to use this in another app as well.

Currently, in that app, I am not using swiftui, I am using firebaseauthui to generate the interfaces for me, and I am not able to snag the display name from that either.

I don’t want to fork it and possibly violate Apple’s design for this feature, but come one, something’s gotta give right.

Any ideas would be most helpful

@Chrichton
Copy link

Chrichton commented Apr 20, 2020

Hi Jamie,

Yes a new user is listed in the authentication tab.
So your problem is the registering.

        Auth.auth().signIn(with: credential) { (result, error) in

should provide you with an error

@jamiedaniel
Copy link

jamiedaniel commented Apr 20, 2020

Ok I have it registering a user and writing to the database. I’m using real-time database because this is an older app when Firestore was in beta.

Anyway. I still do not see how to get the users name from the Sign In with Apple from the Oauth service provider....

The example was confusing for SwiftUI and I get lost when and where I should be asking for the name and how.

Any help will be appreciated.

@Chrichton
Copy link

Chrichton commented Apr 20, 2020

Hi Jamie,
here my code:

private func handleSignInWithApple() {
let nonce = String.randomNonceString()
currentNonce = nonce

    let appleIDProvider = ASAuthorizationAppleIDProvider()
    let request = appleIDProvider.createRequest()
    request.requestedScopes = [.fullName, .email]
    request.nonce = nonce.sha256
    
    let authorizationController = ASAuthorizationController(authorizationRequests: [request])
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

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.
let credential = OAuthProvider.credential(withProviderID: "apple.com",
idToken: idTokenString,
rawNonce: nonce)

        // Sign in with Firebase.
        Auth.auth().signIn(with: credential) { (result, error) in
            if error == nil,
                let fullName = appleIDCredential.fullName,
                let givenName = fullName.givenName,
                let familyName = fullName.familyName {
                    let displayName = "\(givenName) \(familyName)"
                    self.updateDisplayName(displayName: displayName) { result in
                        switch result {
                            case .success(let user): self.handleAuthResultCompletion(user: user, error: nil)
                            case .failure(let error): self.handleAuthResultCompletion(user: nil, error: error)
                        }
                    }
            } else {
                self.handleAuthResultCompletion(user: result?.user, error: error)
            }
        }}
    }

func updateDisplayName(displayName: String, completionHandler: @escaping (Result<User, Error>) -> Void) {
if let user = Auth.auth().currentUser {
let changeRequest = user.createProfileChangeRequest()
changeRequest.displayName = displayName
changeRequest.commitChanges { error in
if let error = error {
completionHandler(.failure(error))
}
else {
if let updatedUser = Auth.auth().currentUser {
print("Successfully updated display name for user [(user.uid)] to [(updatedUser.displayName ?? "(empty)")]")
completionHandler(.success(updatedUser))
}
}
}
}
}

I hope, that helps

@jamiedaniel
Copy link

jamiedaniel commented Apr 20, 2020

Chrichton thanks for the example. I think I am doing something different. I used a custom "canned" login from FirebaseAuthUI as you can see from this code. Here is how I am doing the authentication. The "MyCustomAuthPickerController" all it does is allow me to put a background that I control for the login button picker list from the Authentication Providers. The code for the main ViewController (login services) follows. Feel free to explain where I went wrong ( this was originally written in 2018):

`
//
// ViewController.swift

import UIKit
import Firebase
import FirebaseAuth
import FirebaseUI
import AuthenticationServices

class ViewController: UIViewController, FUIAuthDelegate {

var ref: DatabaseReference!
var databaseHandle: DatabaseHandle!
var userID = ""
var userTokens = ""
var theUser: Firebase.User?

func authUI(_ authUI: FUIAuth, didSignInWith user: FirebaseAuth.User?, error: Error?) {
    if error != nil {
		print("***************************")
        print("There was an error with a login. Error description follows")
        print("***************************")
        print(error?.localizedDescription)
        print("***************************")
    } else {
        
		// add user information to the database
        ref.child("users").child((Auth.auth().currentUser?.uid)!).updateChildValues(["email" : Auth.auth().currentUser?.email! as Any])
        ref.child("users").child((Auth.auth().currentUser?.uid)!).updateChildValues(["provider" : Auth.auth().currentUser?.providerID as Any])
        let name = Auth.auth().currentUser?.displayName
        let splitName = name?.components(separatedBy: " ")
        ref.child("users").child((Auth.auth().currentUser?.uid)!).updateChildValues(["firstname" : splitName?.first ?? ""])
        ref.child("users").child((Auth.auth().currentUser?.uid)!).updateChildValues(["lastname" : splitName?.last ?? ""])
        
        UserDefaults.standard.set(Auth.auth().currentUser?.uid, forKey: Constants.NSUserDefaultsKeys.USER_KEY)
        
        self.loadDashboard()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    configureDatabase()
	
    checkLoggedIn() // Check if there is a user logged in.
    
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
}

func checkLoggedIn() {
    Auth.auth().addStateDidChangeListener{ auth, user in
        if user != nil {
            self.userID = (user?.uid)!
            self.theUser = user
            UserDefaults.standard.set(Auth.auth().currentUser?.uid, forKey: Constants.NSUserDefaultsKeys.USER_KEY)
            
            self.loadDashboard()
            
        } else {
            self.login()
        }
    }
}

//MARK: Load the dashboard after authentication
func loadDashboard() {
    // Load dashboard
    if theUser != nil {
        let dashboardViewController = self.storyboard?.instantiateViewController(withIdentifier: "DashTabBarController")
        dashboardViewController?.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        self.present(dashboardViewController!, animated: true, completion: nil)
    }
}

func login() {
    
    let authUI = FUIAuth.init(uiWith: Auth.auth())
    let providers: [FUIAuthProvider] = [
        FUIGoogleAuth(),
        FUIEmailAuth(),
        FUIOAuth.appleAuthProvider()
    ]
    
    authUI?.providers = providers
    authUI?.delegate = self
    
    
    let authViewController = authUI?.authViewController()
    authViewController?.modalPresentationStyle = .fullScreen
    self.present(authViewController!, animated: false, completion: nil)
    
}

func authPickerViewController(forAuthUI authUI: FUIAuth) -> FUIAuthPickerViewController {
    return MyCustomAuthPickerViewController(authUI: authUI)
}

func configureDatabase() {
    ref = Database.database().reference()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String?
    if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
        return true
    }
    return false
}

}
`

@erhandemirci
Copy link

erhandemirci commented May 11, 2020

Hi @RobSwish, I was able to replicate the issue you encountered, and this is an expected behavior. The fullName is not returned in Apple's ID token so that we can't use it as displayName directly. However, you may opt to get the fullName through "appleIDCredential.fullName" in didCompleteWithAuthorization delegate method, and update user's profile by yourselves.

Additionally, it is mentioned here that Apple only shares the address with apps the first time a user signs in.

appleIDCredential.fullName
worked well.

@MeghaB MeghaB added the Docs label May 14, 2020
@MeghaB
Copy link

MeghaB commented May 14, 2020

For internal tracking, b/156546247

@richlowenberg
Copy link

richlowenberg commented Aug 30, 2020

Is it correct to assume that if we're using the prebuilt UI, there's no way to retrieve the displayName?

@makwanbarzan
Copy link

makwanbarzan commented Oct 22, 2020

Hi @RobSwish, I was able to replicate the issue you encountered, and this is an expected behavior. The fullName is not returned in Apple's ID token so that we can't use it as displayName directly. However, you may opt to get the fullName through "appleIDCredential.fullName" in didCompleteWithAuthorization delegate method, and update user's profile by yourselves.

Additionally, it is mentioned here that Apple only shares the address with apps the first time a user signs in.

This worked for me!

@landnbloc
Copy link

landnbloc commented Nov 19, 2020

@jamiedaniel
Did you ever find a solution to this? or did you have to fork your prepackaged firebaseUI auth? Much Appreciated.
Edit: Nevermind this workaround does it firebase/FirebaseUI-iOS#815 @KrisConrad does it

@unxavi
Copy link

unxavi commented Mar 30, 2021

Just my 2 cents, appleIDCredential.fullName returns the name everytime there is a new SignIn, this seems something that Firebase should fix on the SDK. Then the developer should check if displayName is nil or not and if it is, prompt the user to give you the name. Then user already accept to give you the name with sign in with apple and they can edit the name that they want to give, so this is definitely the name we should use and firebase should store it as the documentation states.

But the minimum should be to update Firebase docs and state that this is not the current behavior. In more than one year has not be possible to update the docs?

@Theunodb
Copy link

Theunodb commented Nov 30, 2021

The values are being returned from Apple, populating it in the displayName would make life much easier:

      String nonce = UtilitiesHelper.randomNonceString();
      AuthorizationCredentialAppleID credential = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
        nonce: crypto.sha256.convert(utf8.encode(nonce)).toString(),
      );

Above returns a credential with the required values from Apple

      final AuthCredential authCredential = OAuthProvider('apple.com').credential(
        idToken: credential.identityToken,
        rawNonce: nonce,
      );

      UserCredential authResult = await FirebaseAuth.instance.signInWithCredential(authCredential);

But it doesn't get added to the authResult values

@mbevin
Copy link

mbevin commented May 8, 2022

Apple is rejecting apps if they don't pre-fill user name fields in-app with the name given by apple during apple authentication, so this really needs to be fixed, as it blocks apps from being successfully submitted to the AppStore.

@sidsarasvati
Copy link

sidsarasvati commented Jun 10, 2022

Is there a agreed upon workaround for getting the display name?

@mbevin
Copy link

mbevin commented Jun 11, 2022

Is there a agreed upon workaround for getting the display name?

This is a patch that I used in a fork I made of https://github.com/firebase/flutterfire.git, which saves the given+family name from the credentials provided by Apple to SharedPreferences, which our app then can retrieve later. It was applied to the flutterfire_ui 0.4.0+5 state - https://gist.github.com/mbevin/393597735c67575eccf0858c4c17168d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests