Flutter/Firebase - Authentication (Phone Authenticate) for predefined user in firebase - firebase

I want to design my Firebase database such that only registered user i.e - I have some predefined set of users who will be using application and only they will be able to sign-in. But the problem is I will get UID only after user is authenticated using OTP in phone authentication and I want to set UID as unique id for each user, So how will admin create the user without UID. I need UID for further operations.
I have one solutions but not sure if this is good idea.
I will create one collection as Registered Users with the phone no as unique ID. and users data like name, email etc. And then when user sign in using phone no. I'll first check in this collection if there phone no is present or not. If it present then I'll allow him to sign-in else not. And with that I'll create another collection as Users and move all the users data from Registered Users collection to Users collection with the UID as unique ID.
Let me know if this is correct approach or is there any better approach available for this kind of scenario. Thanks.

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.

Does the firebase user uid change if the user is signed out of the app when using email authentication?

I have to implement firebase email authentication and want to store data with a field of firebaseUser.uid in that. I want to know if there is any case when the user's uid will be changed so that I don't mess up with the user data as I will be referring it based on the user's uid. So if any user signs up using an email id, it's uid should never change unless he logs in again with a different email.
The UID for a user is assigned randomly and never changes within that project.

Firebase: should I store user data on the authentication profile or database

I signed up a user in my Flutter app using FirebaseAuth.instance.verifyPhoneNumber(). This works perfectly and the user shows up in my Firebase Console.
FirebaseAuth.instance.currentUser() now returns the user for me whenever I open the app. Should I store user info (age, weight etc.) here, or should I create a users collection in my Database and link the currentUser().uid in there?
Also, should I store it on the Database linked against the uid, or linked against the login info. For example, link a user to the phoneNumber and not the uid. Because if you ever delete the user, but they want to register again, then they will get a new uid, but their phoneNumber / email will still stay the same and can therefore still link to their old data.
You can't add arbitrary data to the Firebase Authentication user profile. So you should store the additional data in a database, and indeed associate it with the UID of the user, so that you can easily look it up. This also allows you to implement searches for users more easily, as the client-side Firebase Authentication SDKs have no functionality to look up data for any user but the currently signed in one.
To the additional question: if a user deletes their account from your application, you should respect their wishes and delete the additional data that you store for them too.

How to make auth.uid and database child key equal in firebase?

What's happening here is that I have an app where users just can register if they are invited. By existing users, they need to fill out a form with the invited user's data, and e-mail address. Then these data will uploaded to the firebase database, with an unique generated key by .push(). Then an email is sent by the app to the specified email address, with an unique registration URL. It looks like this: ${host}/register/?uid=${keyGeneratedByPush}. When the invited user clicks on the link, the browser will get the uid from the query string, and gets the data with it from the database. If everything goes well, the user clicks on submit and createUserWithEmailAndPassword() gets called. The problem is the next: the auth() will generate an unique id (uid), but I already have an existing user id (generated by push). What I want to make them the same then if an user logs in, I can get the user.uid, and get the data from the database with it. A possible solution can be to get all the users, loop through them, and find the email address get by the user object, but why if we have ids. So how to make the two ids the same?
You could create a custom authentication provider and use the push ID as your UID there.
But I recommend against doing that. I see the registration/invitation token in the invite as a different entity than the UID that you generate. While (depending on your use-case) there may be a 1:1 mapping between them, the values have different meanings. One identifies a specific invitation sent to a potential user, the other identifies a specific user.
I'd keep a list of the (pending) invitation tokens, and the email address they've been sent to. Then when the user clicks the link in the invitation, you can look up if it's the correct email address (if you care about that), and associate the eventual UID with the invitation code (if you care about that).

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.

Resources