Firebase linking multiple auth providers without matching email - firebase

I've been trying to find a way to figure out if a user is already created with the same email in Firebase, and it doesn't seem like it is possible.
This basically means I will need to save the email for every user and check in the Firebase database if the email is there already.
Is there really no other way?
I see all these posts with... how to link a user with another auth provider, but there is no way to know if the user with the specific mail exists already...

You cannot link an existing account to another account. You can only link a new account to an existing one. If you wish to check if an email of a new account already exists before either creating it or linking it to the existing account.
You can call https://firebase.google.com/docs/reference/js/firebase.auth.Auth#fetchProvidersForEmail
If the email provided already exists, it will return an array of the provider ids. You then sign in the user to the existing account and link the new account to it.
fetchProvidersForEmail will work as expected when multiple accounts per email is disabled in Firebase Console(default behavior unless you are migrating from Firebase v2).

Related

create Firebase account without email and password?

I want to create accounts via Firebase that only create a UID of the user. No password and no email should be requested. The user should be able to delete the app and if he downloads it again, still be able to access his UID and the associated data. In addition, this registration should never expire and the UID of the user should always remain unique and not be overwritten.
Is there a tool in Firebase that can be used to do this?
What you're describing is known as an anonymous account in Firebase, and you can create one with a single call as shown in documentation for iOS, Android, and Web.

Firebase Authentication with popup - allow only registered user

I wanted to create website where I have separate Sign In and Sign Up form. I also want to have Google authentication with Firebase.
I've implemented it like this both on sign in and sign up page:
await FIREBASE_AUTH.signInWithPopup(googleAuthProvider);
But this code will always create new user.
What I would like to do is to block creating new user on sign in page, only log them in if user already exists (e.g. as I require terms of use consent on sign up page, but I don't want to require it on sign up page - it would be quite weird)
There is no way in Firebase Authentication to prevent users from signing up, while still allowing them to sign in if they're already created. The reason for this is that Firebase Authentication merely focuses on allowing users to authenticate themselves, so to prove who they are by providing credentials. What they can then do in your app is known as authorization, and is up to you to implement in your front-end application code, back-end application code, and server-side security rules.
For example, if you use one of Firebase's databases (Cloud Firestore or Realtime Database), you'll typically maintain a list of approved user's in that list (either their email or their UID). Then before granting the user access to specific screens in your app or data in your database, you check if the users exists in that list. If not, you don't grant access to the screen or data.
I don't see an issue here, when a user uses google auth when they already have an account it will create a new account with their in some cases new data he might have changed in his google account.
In case your users hold other data in your database I'm pretty sure there's a google auth API for that issue.

Firebase authentication with multple providers

I'm trying to create a user authentication screen, so I decided to use Firebase.
However, I am facing a problem. Nowadays I have enabled 2 providers, which are:
1- E-mail/Password
2- Google
Enabled providers
I can create users perfectly.
When I create a user with E-mail/password option and use the Gmail address guilherme.nunes#fover.com.br, Firebase creates the user normally.
After using email, I am trying to use the second option of Google provider, using the same email. Now the old user is replaced with the new provider in Firebase.
account created
After this, if I try to login with the first option (E-mail/Password) again, it says that the user does not exist.
I tried disabling the option to have 1 user per account, but then, Firebase creates 2 distinct users.
What I would like, is to link 1 account, with several providers.
Can someone help me, please?
When you enable the option to allow only a single account per email address, Firebase has certain providers that are preferred for certain email domains. Most notably: the Google provider is the preferred provider for #gmail.com addresses.
So in your initial process, you:
Register a user something#gmail.com with a email+password account.
Register a user something#gmail.com with a google account.
In this case, the second registration replaces/overrides the initial account.
What you want to do is link the two accounts/providers as described in the documentation on account linking. With this approach you don't fully register an account in step 2, but instead link the credentials for that second account to the email+password account that was already created for the user.
The documentation has a step-by-step explanation on how to do this. If you're having trouble making this work, post a new question with the minimal, complete/standalone code that reproduces where you got stuck.

Why use UID in Firebase? Should I use it

I know UID is used because it is unique. But in my app, all of them are registered with Google ID, Google ID is also unique. Should I use UID?
yes it is better to use the uid.
From the docs:
You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app.
So after you authenticate the users, the uid will be in the authentication page in firebase. That id will help you later in the firebase database also and it is easier to use and add in the database.
Can easily be gotten using this:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
Then you can use the method getUid() to get the userid. So using it will make the work easier for you.
From the docs:
After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.
Also check this link: https://firebase.google.com/docs/auth/android/google-signin (Next Step section)
I'll suggest you use email ID instead of UID because if the user account is deleted from your Firebase Auth (either you delete it using Admin SDK, or perform a manual deletion on console), the next time user signs in with the same email ID will now give you a different UID and therefore all of your data in your database which rely on your UID won't be accessible.
However, you can't use use an email ID as it is, because Firebase key doesn't allow you to use . (dot) as keys, so just replace your . with a ,. You can find more information here.
TL;DR
Use email ID as it will always be unique unlike UID which gets generated every time a user signs in if that ID was previously deleted on Firebase Authentication server.

How many time Anonymous users are created in Firebase

I would like to use the signInAnonymously feature from firebase but I'd like to know how it works exactly.
Is this anonymous user added as any other user in Firebase (but without an email or password) ?
Is it automatically removed after a certain time ?
If I call this method on each app launch. Will it create a new user everytime ? How will it know how te reuse an old one ? Local Storage ?
So will it create another user in database if my users connects from another device ?
Thanks a lot
An anonymous user account is similar to any other user account in Firebase, except that there is no way to identify the user.
Anonymous users are not automatically removed.
Each time you call the API to create an anonymous user, a new account will be created. To re-use the existing account, you should listen for the auth state when the app starts and only create a new user account if the user isn't signed in yet.
If the same (anonymous) user signs in from multiple devices, they will get a separate account on each. If that is not what you want for your use-case, you should require one of the identified authentication methods (email/password, Facebook, Google, etc)/
Frank's answer to the third question is incorrect.
From the documentation:
If there is already an anonymous user signed in, that user will be returned instead. If there is any other existing user signed in, that user will be signed out.
Therefore, this method can be safely called every time the application starts.

Resources