How to get user email in Firebase based on user ID

I need to get a user object, in particular the user's email address, I will have a user ID in this format:

simplelogin:6

So I need to write a function something like this:

getUserEmail('simplelogin:6')

Is it possible?

+10
source share
5 answers

To get the email address of the current user, use the getAuth function. For email and / simplelogin password, you can receive email as follows:

 ref = new Firebase('https://YourFirebase.firebaseio.com'); email = ref.getAuth().password.email; 

In my opinion, the password object is not very precisely named because it contains an email field.

I believe that the Firebase function does not get the email address of any user using uid. Of course, this will expose the emails of all users to all users. If you want to, you will need to save each user’s email in the database by their uid during account creation. Then other users will be able to receive the email from the database using uid.

+7
source

It is possible with the admin SDK

The admin SDK cannot be used on the client, only in the Firebase cloud functions, which can then be called from the client. You will be given the following promises: (It’s very easy to configure the cloud function .)

 admin.auth().getUser(uid) admin.auth().getUserByEmail(email) admin.auth().getUserByPhoneNumber(phoneNumber) 

look here https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data


In short, this is what you are looking for

 admin.auth().getUser(data.uid) .then(userRecord => resolve(userRecord.toJSON().email)) .catch(error => reject({status: 'error', code: 500, error})) 

full fragment

In the code below, I first check that the user calling this function has the right to display such confidential information about someone by checking if his uid is under the userRights/admin node.

 export const getUser = functions.https.onCall((data, context) => { if (!context.auth) return {status: 'error', code: 401, message: 'Not signed in'} return new Promise((resolve, reject) => { // verify user rights admin.database().ref('userRights/admin').child(context.auth.uid).once('value', snapshot => { if (snapshot.val() === true) { // query user data admin.auth().getUser(data.uid) .then(userRecord => { resolve(userRecord.toJSON()) // WARNING! Filter the json first, it contains password hash! }) .catch(error => { console.error('Error fetching user data:', error) reject({status: 'error', code: 500, error}) }) } else { reject({status: 'error', code: 403, message: 'Forbidden'}) } }) }) }) 

By the way, read about the difference between onCall() and onRequest() here .

+5
source

just get a firebaseauth instance. I created one default email and password in firebase. this is only for security, so that no one can use it, except for those who know or who bought our product to use our application. The next step is to provide a screen for creating a user account.

 FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); String email = user.getEmail(); 

every time a user opens the application, the user is redirected to the dashboard if the current user is not equal to our default email. below is the code

 mAuth = FirebaseAuth.getInstance(); if (mAuth.getCurrentUser() != null){ String EMAIL= mAuth.getCurrentUser().getEmail(); if (!EMAIL.equals(" example@gmail.com ")){ startActivity(new Intent(LoginActivity.this,MainActivity.class)); finish(); } } 

i I also searched for the same solution, finally got it.

+3
source

Current Solution (2019) for Firebase:

 firebase.auth().currentUser && firebase.auth().currentUser.email 

see: https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#currentuser

Not every provider will have a specific email value, but email authentication will.

+2
source

Current Solution (Xcode 11.0)

 Auth.auth().currentUser? ?? "Mail" Auth.auth().currentUser?.email ?? "User" 
0
source

Source: https://habr.com/ru/post/1014116/


All Articles