How to change email in firebase auth?

前端 未结 4 2019
-上瘾入骨i
-上瘾入骨i 2020-12-29 20:08

I am trying to change/update a user\'s email address using :

firebase.auth().changeEmail({oldEmail, newEmail, password}, cb)

But I am getti

相关标签:
4条回答
  • 2020-12-29 20:54

    You can do this directly with AngularFire2, you just need to add "currentUser" to your path.

    this.af.auth.currentUser.updateEmail(email)
    .then(() => {
      ...
    });
    

    You will also need to reauthenticate the login prior to calling this as Firebase requires a fresh authentication to perform certain account functions such as deleting the account, changing the email or the password.

    For the project I just implemented this on, I just included the login as part of the change password/email forms and then called "signInWithEmailAndPassword" just prior to the "updateEmail" call.

    To update the password just do the following:

    this.af.auth.currentUser.updatePassword(password)
    .then(() => {
      ...
    });
    
    0 讨论(0)
  • 2020-12-29 20:56

    If someone is looking for updating a user's email via Firebase Admin, it's documented over here and can be performed with:

    admin.auth().updateUser(uid, {
      email: "modifiedUser@example.com"
    });
    
    0 讨论(0)
  • 2020-12-29 21:01

    You're looking for the updateEmail() method on the firebase.User object: https://firebase.google.com/docs/reference/js/firebase.User#updateEmail

    Since this is on the user object, your user will already have to be signed in. Hence it only requires the password.

    Simple usage:

    firebase.auth()
        .signInWithEmailAndPassword('you@domain.com', 'correcthorsebatterystaple')
        .then(function(userCredential) {
            userCredential.user.updateEmail('newyou@domain.com')
        })
    
    0 讨论(0)
  • 2020-12-29 21:09

    updateEmail needs to happen right after sign in due to email being a security sensitive info
    Example for Kotlin

     // need to sign user in immediately before updating the email 
            auth.signInWithEmailAndPassword("currentEmail","currentPassword")
            .addOnCompleteListener(this) { task ->
                    if (task.isSuccessful) {
                        // Sign in success now update email                
                        auth.currentUser!!.updateEmail(newEmail)
                            .addOnCompleteListener{ task ->
                            if (task.isSuccessful) {
                   // email update completed
               }else{
                   // email update failed
                        }
           }
           } else {
                        // sign in failed
                    }
                }
    
    0 讨论(0)
提交回复
热议问题