1

I want to provide users multiple authentification providers with phone, facebook and a simple email and password.

the thing is, i want the same account to be provided (same user id, same display name) if the account is linked with the same Email address.

as i got to understand it through the documentation, you can do it if the user connects and link an account when already connected with another provider.

so my question is : is there anyway to do it automatically, when let's say, the user connects with another provider next time, without having to tell him to link it himself.

1 Answers1

0

[Edit - after further investigation, I found a better answer]

It turns out that only allowing the email address once comes with some caveats - that are really well laid out in this GitHub issue but I'll try to summarize.

You can have an unverified account, which may allow you to register over it (the user hasn't proved that they own the account).

Next are kind of the social identity providers. Facebook is assumed to have verified your email once (for example), so you may get a duplicate account exception.

Finally, there are email identity providers - these are seen as authoritative for all email addresses that they're associated with and will overwrite any account with the same address for the domains that they're authoritative over. Both Google and Apple sign ins have this control over their respective domains for example.

See this section of the web documentation and this and this stack overflow answers.

At this point, you may be wondering how to get more control over the process if this isn't the user flow you want.

You may opt to make a call to FirebaseAuth.DefaultInstance.FetchProvidersForEmailAsync before allowing the user to log in. Then you can choose to prompt them to log back in with their existing account first to link accounts before creating a new one (and potentially replacing the old account). For example:

public async void LogIn(string email, Credential credential) {
    var auth = FirebaseAuth.DefaultInstance;
    var identityProviders = await auth.FetchProvidersForEmail(email);
    if (identityProviders.Empty()) {
        var user = auth.SignInWithCredentialAsync(credential);
    }
    else {
        // somewhere in here, log in the user and call
        // user.LinkWithCredentialAsync(credential)
        DisplayLinkAccountDialog(identityProviders, email, credential);
    }
}

[Old answer - information still applicable but incomplete]

By default, Firebase authentication only allows one email address per user. See this section at the bottom of your Authentication Providers page in the Firebase console:

One account per email option in the Firebase console

So when you sign up a new user, you should get a FirebaseException with the error code AccountExistsWithDifferentCredentials.

To handle this, you'd make your continuation look something like:

/* Registration call */.ContinueWithOnMainThread(task=>{
    if (task.Exception) {
        foreach (var e in task.Exception.InnerExceptions) {
            var fe = e as FirebaseException;
            if (fe != null) {
                if (fe.ErrorCode == AuthError.AccountExistsWithDifferentCredentials) {
                    TryToLinkInsteadOfCreateAccount(); // TODO - cache credentials and implement this
                }
            }
        }
    }
});

Let me know if that helps!

--Patrick

Patrick Martin
  • 2,993
  • 1
  • 13
  • 11
  • Thank you for reply However, i do have that option in the firebase console by default, but it still does accept 2 accounts with the same email, one from facebook and one with email and password. – Med Lotfi Gouasmi Jun 18 '20 at 14:36
  • I did find a solution about linking accounts with differents providers as i will prompt users to verify their account's phone number, and thus link them with credentials. but phone verifications wouldn't work with my phone, it works with some others, which is wierd, providing i tested on the same phone operator. – Med Lotfi Gouasmi Jun 18 '20 at 14:53
  • i get this message on Logcat : Attempt to remove non-JNI local reference, dumping thread. is this problematic ? – Med Lotfi Gouasmi Jun 18 '20 at 16:01
  • Nevermind again i managed to fix it, i had a problem with the app signature but now it's fixed thanks for the reply anyway. – Med Lotfi Gouasmi Jun 18 '20 at 18:12
  • I also did a bunch of digging around and got some more detailed context around auth. I hope that all helps! – Patrick Martin Jun 18 '20 at 20:05
  • Why did i never thought about the possiblity of having a user input an email address that he doesn't own ? makes so much sense to why use email verification lol thank you so much, you saved me a lot of time there ! – Med Lotfi Gouasmi Jun 19 '20 at 23:20
  • No problem, I'm glad to have helped! – Patrick Martin Jun 22 '20 at 15:13