Ronnie Smith
Ronnie Smith

Reputation: 18555

Firebase Auth : Linking Anonymous Account

The documentation Convert an anonymous account to a permanent account indicates 3 steps to the process, however step 2 seems to violate step 1.

  1. When the user signs up, complete the sign-in flow for the user's authentication provider up to, but not including, calling one of the Auth.signInWith methods. For example, get the user's Google ID token, Facebook access token, or email address and password.
  2. Get an AuthCredential for the new authentication provider: var credential = firebase.auth.FacebookAuthProvider.credential( response.authResponse.accessToken);
  3. Pass the AuthCredential object to the sign-in user's link method...

My question is re: step 2. response exists only after calling one of the Auth.signInWith methods that step 1 says not to do. How does one link an anonymous account with an oAuth account?

Upvotes: 6

Views: 2620

Answers (1)

Ricardo Smania
Ricardo Smania

Reputation: 3139

What the documentation says is that in step 1 you should not sign in with Firebase. What step 1 is saying is that you should sign in with the provider (i.e. Google, Facebook, Twitter), and then get the provider token and then link the provider token to an existing Firebase anonymous account or sign in with Firebase using the provider token. The important thing is that the token the documentation refers to comes from the provider, not Firebase.

You didn't specify in what language/platform you plan to do this, but here is just an example with Dart/Flutter:

GoogleSignInAccount googleUser = await _googleSignIn.signIn();
// Get the provider auth token
GoogleSignInAuthentication googleAuth = await googleUser.authentication;     
FirebaseUser user = await _auth.currentUser();
// Check if the user has signed in as anonymous
if (user != null) { 
  // Use the provider auth token to link the anonymous account
  await _auth.linkWithGoogleCredential(
      accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
} else if (user == null || user.email == null) {
  user = await _auth.signInWithGoogle(
    accessToken: googleAuth.accessToken, 
    idToken: googleAuth.idToken,
  );
}

Upvotes: 5

Related Questions