I'm developing an app with react native(0.61.5)
and I'm trying to implement several log in using react-native-firebase/auth(6.3.4)
For the log in with apple I use react-native-apple-authentication it's working like a charm following this example : https://github.com/invertase/react-native-apple-authentication/blob/master/docs/FIREBASE.md
const _signInUserWithCredential = (credential: FirebaseAuthTypes.AuthCredential) => { firebase .auth() .signInWithCredential(credential) .then(data => { //success }) .catch(error => { console.warn(error); });};
My user can logIn with several credential (Google, Apple, Facebook...). If my user is already loged with a credential I use _linkUserWithCredential
to link the new credential to the existing account :
const _linkUserWithCredential = (credential: FirebaseAuthTypes.AuthCredential) => { if (user !== null) { user.linkWithCredential(credential) .then(data => { //success }) .catch(error => { console.warn(error.code); }); }};
It works too.
I have an issue when I try to merge an Apple account with an existing one.
As explained in the doc
:https://firebase.google.com/docs/auth/web/account-linking
Account linking will fail if the credentials are already linked to another user account. In this situation, you must handle merging the accounts and associated data as appropriate for your app
var prevUser = auth.currentUser;// Sign in user with another accountauth.signInWithCredential(credential).then(function(user) { console.log("Sign In Success", user); var currentUser = user; // Merge prevUser and currentUser data stored in Firebase. // Note: How you handle this is specific to your application // After data is migrated delete the duplicate user return user.delete().then(function() { // Link the OAuth Credential to original account return prevUser.linkWithCredential(credential); }).then(function() { // Sign in with the newly linked credential return auth.signInWithCredential(credential); });}).catch(function(error) { console.log("Sign In Error", error);});
and again it's working except with Apple
Apple credential can only be use once so if linkWithCredential
fails I can't merge the account as I need a new credential to perform signInWithCredential
.
In the iOS firebase doc you can retrieve new credential from the error fired by linkWithCredential
in the userInfo
key FIRAuthErrorUserInfoUpdatedCredentialKey
as explain here : https://firebase.google.com/docs/auth/ios/apple
Unfortunately I think that FIRAuthErrorUserInfoUpdatedCredentialKey
is not implemented for react native.
So my questions are :
- How can I get a new apple credential without having my user to prompt again his password?
- How can I reuse an apple credential ?
- Do you have any idea to merge an apple user to an existing one?
A work around would be to use firebase admin to test if a user exists with the current credential and then merge the accounts but I'd rather not install firebase admin just for that