6
votes

So I saw here: https://firebase.google.com/docs/auth/web/account-linking#link-auth-provider-credentials-to-a-user-account That it is now possible to link user accounts in Firebase. I also saw that Firebase provides the functionality of anonymous authentication, where it creates a user session for a user, without any credentials.

In our application we normally use CustomAuthentication with Firebase, as we have our own authentication service. This works perfectly, and we are able to use the same Auth system in-between systems that use Firebase, and the ones that don't.

Now we got to the point where we wanted to take advantage of the anonymous authentication of Firebase, to allow users to use the apps without registering, and just transfer their details after they log in. So I thought that account linking is what I need. But I can't find a way to link an anonymous account with a custom authentication account. Is something like this possible with Firebase?

3

3 Answers

7
votes

I have not found linking between anonymous and custom signIns too, so I just use signInAnonymously and pass it's uid to signInWithCustomToken.

Step 1. To be able acheive this, you should grab an uid from signInAnonymously function like this:

var uid;
auth().signInAnonymously().then(user => {
  uid = user.uid;
});

Step 2. Save current user data like this:

database().ref(`users/${uid}`).set({ some: 'data' });

Step 3. Send this uid to your server which returns custom token. On your server you just pass this uid to firebase.auth().createCustomToken(uid) function and send this custom token back. It will contain the uid.

Step 4. When you receive custom token, you can sign in with it like this:

auth().signInWithCustomToken(authKey);

And that's it. You are signed in with your custom token and can access your anonymous user data saved on the step 2.

2
votes

I had a similar problem but I believe Firebase does not allow this type of linking because it would require linking two separate firebase user ids: the id of the custom token created user and the id of the anonymous user.

Firebase docs say:

Users are identifiable by the same Firebase user ID regardless of the authentication provider they used to sign in.

So handling linking between two user ID's would cause inconsistencies.

I got around this problem by doing a merge of the data between the two accounts.

For example (from Firebase docs):

// Get reference to the currently signed-in user
var prevUser = auth.currentUser;
// Sign in user with another account
auth.signInWithCredential(credential).then(function(user) {
  console.log("Sign In Success", user);
  var currentUser = user;
  // Merge prevUser and currentUser accounts and data
  // ...
}, function(error) {
  console.log("Sign In Error", error);
});
-1
votes

Caveat: I've never done this. Have you seen this page? Down at the bottom, under the heading "Email-password sign-in", it lists this code fragment:

var credential = firebase.auth.EmailPasswordAuthProvider.credential(email, password);

You can then (apparently) link the credential like this:

auth.currentUser.link(credential).then(function(user) {
  console.log("Anonymous account successfully upgraded", user);
}, function(error) {
  console.log("Error upgrading anonymous account", error);
});