How to get the email of any user in Firebase based on user id?

前端 未结 5 1677
灰色年华
灰色年华 2020-12-09 04:48

I need to get a user object, specifically the user email, I will have the user id in this format:

simplelogin:6

So I need to write a function so

相关标签:
5条回答
  • 2020-12-09 05:16

    It is possible with Admin SDK

    Admin SDK cannot be used on client, only in Firebase Cloud Functions which you can then call from client. You will be provided with these promises: (it's really easy to set a cloud function up.)

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

    See 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 snippet

    In the code below, I first verify that the user who calls this function is authorized to display such sensitive information about anybody by checking if his uid is under the node userRights/admin.

    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's 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'})
          }
        })
      })
    })
    
    

    BTW, read about difference between onCall() and onRequest() here.

    0 讨论(0)
  • 2020-12-09 05:17

    Current solution as per latest update of Firebase framework:

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

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

    Every provider haven't a defined email address, but if user authenticate with email. then it will be a possible way to achieve above solution.

    0 讨论(0)
  • 2020-12-09 05:21

    To get the email address of the currently logged in user, use the getAuth function. For email and password / simplelogin you should be able to get the email like this:

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

    In my opinion, the password object is not very aptly named, since it contains the email field.

    I believe it is not a Firebase feature to get the email address of just any user by uid. Certainly, this would expose the emails of all users to all users. If you do want this, you will need to save the email of each user to the database, by their uid, at the time of account creation. Other users will then be able to retrieve the email from the database by the uid .

    0 讨论(0)
  • 2020-12-09 05:25

    Current solution (Xcode 11.0)

    Auth.auth().currentUser? ?? "Mail"
    Auth.auth().currentUser?.email ?? "User"
    
    0 讨论(0)
  • 2020-12-09 05:27

    simple get the firebaseauth instance. i created one default email and password in firebase. this is only for the security so that no one can get used other than who knows or who purchased our product to use our app. Next step we are providing singup screen for user account creation.

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

    every time user opens the app, user redirecting to dashboard if 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 Am also searching for the same solution finally i got it.

    0 讨论(0)
提交回复
热议问题