Firebase: Get DisplayName from UID [duplicate] - android

This question already has answers here:
How do I return a list of users if I use the Firebase simple username & password authentication
(7 answers)
Closed 6 years ago.
I am storing user's display name in Firebase Auth. Fetching from Firebase Auth is simple when the same user is logged in. But, when another user is logged in, how do I fetch an account's display name when I have their unique UID (User ID).

One solution is when you register a user add his details in database also
Example
Once you are under createUserWithEmailandPassword()
DatabaseReference db=FirebaseDatabase.getInstance().getReference();
DatabaseReference users = db.child("Users").child(mAuth.getCurrentUser().getUid());
users.child("Name").setValue("<NAME>");
Now when the user signs into your database retrieve the Name from the database from child Users

You can't get the name, or any other details of a user that is not currently signed in using FirebaseAuth.
Instead, you must create a node in your database where you store the name, and any other necessary details by querying the database.
I'm really curious to know how you have the UID of any other user without doing this.

Related

Flutter store users data with Cloud Firestore and Firebase Auth

I have a collection of "Users" in Cloud Firestore and I am using Firebase Auth to authenticate my app users as follows:
The user authenticates within the mobile app (for example with Google).
I verify that there is no document within the "Users" collection that corresponds to the UID of the authenticated user.
If the document does not exist in "Users" I use the user's UID to create a new document.
It is my question: Is there a security problem with this model or some other type?
I am confused by Google documentation because it says that the user's UID is unique to the Firebase project but should not be used to authenticate my user to the backend server. It also says that in that case, I should use FirebaseUser.getToken () but that token can change, so it will create a new user in my DB.
So, my second question is: When should you use that token? Give me an example, please.
Thanks for the help.
USER_ID
You can use the google generated user_id for future reference. That will never change for a given user email or authentication type as long as the user is already in the database.
Example:
If the user Signed In with google federated login with user1#example.com then the user record is maintained in the Firebase and USER_ID(Say ABFDe12cdaa2...) will be assigned(You can use this id in URLs to see the user profile etc it is kind of 32 chars long(I am not sure exactly here). Now, If a user tries to sign up again with the same email(user1#example.com) then it pulls the previous record ABFDe12cdaa2.... If you delete a user1#example.com from the firebase database(Firebase maintains its own database of the user for your project, With has a limited number of user properties). Then the user tries to sign in again then the new USER_ID is generated.
Now the TOKEN:
As you USER_ID is public, it can be seen by everyone. It is not used for authentication.
So you need to generate the token( This is longer the user id) to authenticate programmatically with the Firebase. It token is temporary and specific to the user. It will expire in some time ( you can define that time while creating the token). a refresh token is used to get a new token.
I don't have any code examples while writing this answer. I will update with code example,If, I find any.
Hope I clarified some of your questions.

Storing the data of each user uniquely [duplicate]

This question already has answers here:
How do I link each user to their data in Firebase?
(2 answers)
Closed 3 years ago.
In case of Login and Registration app, how can we store the details of each user uniquely in firebase real-time database? when I try to login with a different user the data is being overwritten.
How can I Solve This?
If you are logged in with a user with FirebaseAuth you can use getUid() to get a unique identifier for that logged in user, then you can write to the database with that UID, the next time a user logs in with the same account, it will write on that same unique destination. If the user logs in with another account, another new unique identification will be created for that specific account.
Example
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance().getCurrentUser();
String userID = mAuth.getUid();
mDatabaseRef.child("semOneOne").child(userID).child("name").setValue("Gastón");
Important: Sometimes getCurrentUser() could be null, and
so on your userID, so, make sure you check if the user is logged in
first before getting the userID
For more information please check on how to Manage Users in Firebase

Firebase check if user exists by display name [duplicate]

This question already has answers here:
How to get userID by user Email Firebase android?
(6 answers)
Closed 5 years ago.
I am trying to know if a user exists in Firebase (Email/Password Authentication) y only knowing the display name.
I am doing this is Android (Java).
Thank you so much!
There is no built-in support in Firebase Authentication to look up users by their display name. Given the loose definition of the meaning of the display name, it would be of limited use. Display names can't be required to be unique, since it's just a value that says how the user wants to be addressed: "When you show something from/to me, show this name next to it".
Many developers end up allowing their users to pick a username/nickname, which they then do require to be unique. If that approach make sense for your use-case too, I recommend reading some of these questions about how to accomplish this with the use of the Firebase Realtime Database:
Firebase android : make username unique
Enforcing unique usernames with Firebase simplelogin
Firebase query if child of child contains a value (a more general explanation of uniqueness in the Firebase Realtime Database)
How do you prevent duplicate user properties in Firebase?

Retrieve list of user from Firebase Authentication [duplicate]

This question already has answers here:
How do I return a list of users if I use the Firebase simple username & password authentication
(7 answers)
Closed 5 years ago.
How can I retrieve the list of users from Firebase Authentication? I can get the 'current' user but that's it. Or do I have to add users to database first and get the list from there?
If you have a nodejs project, use the firebase auth:export to export users from Firebase Auth. Users can be exported in csv or json format:
firebase auth:export users.json --format=json --project projectname
It's not possible to retrieve a list of users from Firebase Authentication.
The explanation is in this thread
The only way to do that is to save the user to the database once they are registered.

How do I search my Firebase database for a certain key in my Android app?

I am currently making an Android app that allows Facebook sign-in and for each user, stores their email address (from Facebook) along with a couple integer variables in my database.
Upon login, I want my app to check if my database contains the email address of the logged on user. If it does, I proceed with the app functionality, if not, I create a new entry in my database for that user, with an email address and the associated integers.
How can I formulate a query or request that searches my database for a given email string? I am not very experienced with Firebase.
Using FirebaseAuth for your Facebook sign-in would give every user a unique id generated by Firebase.
This id is returned to you in the code when login is successful. You could then retrieve the user's email from the returned object.
Assuming you're using Firebase's real time database to store your info, you could check out their official docs here on how to use your DatabaseReference DataSnapshot to query the database

Categories

Resources