简体   繁体   中英

firebase react-native: convert anonymous user to a permanent user via phone auth

My react-native application allows users to sign in anonymously using firebase. After that I am hoping to allow user to create an account via firebase phone auth and convert this anonymous account into a permanent account.

I think this is possible based on the api & documentation provided by firebase

anonymous account creation: https://rnfirebase.io/docs/v5.xx/auth/reference/auth#signInAnonymously

phone auth creation & retrieve credential: https://rnfirebase.io/docs/v5.xx/auth/reference/PhoneAuthProvider#credential

Linking an existing user with a new credential: https://rnfirebase.io/docs/v5.xx/auth/reference/User#linkWithCredential

https://firebase.google.com/docs/auth/web/account-linking

Here's what I did

anonymous account creation:

const {user: {uid: userID}} = await firebase.auth().signInAnonymously();

// successfully created and save this user's id

get the users to authenticate and get the phone auth credential:

const credential = firebase.auth.PhoneAuthProvider.credential(verificationId, code);

at this point I would have a valid credential & an existing anonymous user ready for linking

I then provide the phone auth credential to my anonymous user for linking

const authUser = await firebaseAuth.currentUser.linkWithCredential(credential);

I expected a successful linking to happen based on my inputs, but I'm getting errors instead, the error I'm getting is

Error: This credential is already associated with a different user account.

Error code is: ERROR_CREDENTIAL_ALREADY_IN_USE

in the documentation under ERROR_CREDENTIAL_ALREADY_IN_USE , they did mention:

this error could be thrown if you are upgrading an anonymous user to a Google user by linking a Google credential to it and the Google credential used is already associated with an existing Firebase Google user.The fields error.email, error.phoneNumber, and error.credential (AuthCredential) may be provided, depending on the type of credential. You can recover from this error by signing in with error.credential directly via auth#signInWithCredential.

But I have not been able to find error.credential under the error object to come back from the error.

I have seen many working example of linking anonymous user with other auth providers, but never seen the linking of a firebase anonymous user with a firebase phone auth crendential. Does anyone know if this type of linking is supported? Could anyone point out what I'm doing wrong here?

Follow up:

So I actually found out that my code did work, and that is how you'd implement firebase anonymous user -> firebase phone auth account linking.

The reason this didn't work for me was because..... for our app, we had a micro-service creating accounts in the backend every time the user inputs the confirmation code. In hindsight I should have really double checked our own codebase, lesson learned.

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