简体   繁体   中英

How do you update a user in Firebase using AngularFire2?

I can easily use this to create a user:

this.af.auth.createUser({email: email, password: password});

but how do I edit the users details once they're created? (ie: Change the password or the e-mail address?). I would think something like this:

this.af.auth.updateUser({email: email, password: password});

But there's no updateUser method?

As discussed here , you can subscribe to a FirebaseAuthState observable and use Firebase's updateProfile() method.

private authState: FirebaseAuthState;

constructor(private af: AngularFire) {
    this.af.auth.take(1).subscribe(auth => {
        this.authState = auth;
}

authState.auth.updateProfile({
  displayName: 'display name',
  photoURL: 'some/url'
}).then(() => {
  ...
});

I have once faced this problem and have not got an angularfire2 answer yet.

Here is a native firebase way I get through this problem.

public auth: any;
constructor(@Inject(FirebaseApp) firebaseApp: any) {
  this.auth = firebaseApp.auth();
}

reset(targetEmail: any) {
  this.auth.sendPasswordResetEmail(targetEmail);
}

Note:

It seems we could not change the password directly, only to send a password reset eamil to the target email address.

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(() => {
  ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM