简体   繁体   中英

Firebase doesn't update email of Facebook user after Facebook primary email changes

I have a test Firebase app with Facebook login enabled. I created a test FB account (call it abc@xyz.com ), logged into Firebase through FB login, requested FB profile data like id and email through a GraphRequest using the accessToken returned, and everything seems correct.

Then I added another email to the FB account (call it def@uvw.com ), changed the primary email from abc@xyz.com to def@uvw.com , and removed abc@xyz.com from the account. So technically, abc@xyz.com is now released and not associated with the FB account.

Now when I login to Firebase with def@uvw.com Firebase correctly recognizes that it's the same user that logged in before (since FB credentials are the same). However, when I see the users list in the Firebase console, the email of the user still shows abc@xyz.com .

So I created a new FB account with abc@xyz.com and tried to login to my Firebase app with that. But since I've chosen the "One account per email address" option in Firebase Authentication settings, I get the following error:

Error Domain=FIRAuthErrorDomain Code=17012 "An account already exists with
the same email address but different sign-in credentials. Sign in using a 
provider associated with this email address." UserInfo=
{error_name=ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL, 
FIRAuthErrorUserInfoEmailKey=abc@xyz.com, NSLocalizedDescription=An 
account already exists with the same email address but different sign-in  
credentials. Sign in using a provider associated with this email address.}

This means that even though I've released that email from the FB account, and created a new user with that released email, I can't ever login to my app again (as long as if I've chosen the "One account per email address" option in Firebase Authentication settings)

So I'm curious: is there a reason Firebase doesn't update the email on the first account after the primary email on the FB account changes? Is it a bug? Because the GraphRequest response returns the new email, so Firebase should technically be able to update it if there's no other user with the new email already registered.

I suppose Firebase has no idea that the email has changed and can't track it, because Firebase doesn't have permissions to access user's account details, only your app can do this.

You need to manage email change scenario on your own. Here's how I do this on iOS:

1) each time user opens the app I'm doing a Graph API request to fetch fresh user's data. The email returned by this request is compared with the email stored in Firebase for this user.

2) once I detect that the emails are different - I'm updating the email stored in Firebase to be in sync with the Facebook's one. In iOS SDK it is:

Auth.auth().currentUser?.updateEmail(to: newEmail) { error in
    // if there was no error - the firebase now has the latest email
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM