【发布时间】:2018-01-09 11:48:50
【问题描述】:
我正在努力实现的目标
- 一个简单的更新电子邮件和密码“表格”。
问题
- 当我最近登录时,更改电子邮件工作正常
- 当我最近没有进行身份验证时,会抛出错误
问题
- 如何使用“reauathenticateWithCredential”方法?
- 我应该在哪里存储用户密码以传递给方法?
更改电子邮件表单
saveNewEmailAddress(email: string) {
this.authService.updateEmailAddress(email);
}
身份验证服务
电子邮件似乎出现在 intelliSense 中(所以我希望它有效 :))。但是,如何传递密码?
updateEmailAddress(email: string) {
const currentUser = firebase.auth().currentUser;
const credentials = firebase.auth.EmailAuthProvider.credential(currentUser.email, password);
currentUser.reauthenticateWithCredential(credentials).then(function () {
currentUser.updateEmail(email).then(function () {
// Success
}).catch(function (error) {
// An error happened.
});
})
}
这里的任何帮助、代码示例、建议将不胜感激。对此非常陌生。
更新
这就是我让它工作的方式。请告知这是否好。我改变的是:
- 尝试更改电子邮件时,我会列出当前电子邮件、新电子邮件输入字段和密码字段
- 密码字段用于当前密码
- 输入新的电子邮件地址和密码后,“保存”按钮就会启用
- 我获取新的电子邮件和密码并将它们传递给身份验证服务,在该服务中我运行更新电子邮件的方法
帐户组件 HTML
<!-- Password -->
<div class="form-group" [ngClass]="{'has-error': (customerForm.get('password').touched || customerForm.get('password').dirty) && !customerForm.get('password').valid }">
<label class="col-md-2 control-label" for="passwordId">Password</label>
<input #currentPassword class="form-control" id="passwordId" type="text" placeholder="Password(required)" formControlName="password"/>
<span class="help-block" *ngIf="(customerForm.get('password').touched || customerForm.get('password').dirty) && customerForm.get('password').errors">
<span *ngIf="customerForm.get('password').errors.required">
Please enter your Password.
</span>
<span *ngIf="customerForm.get('password').errors.minlength"> The password must be longer than 10 characters. </span>
</span>
</div>
<!-- Save Button -->
<div class="form-group">
<button class="btn btn-primary" type="submit" [disabled]="!customerForm.valid" (click)="saveNewEmailAddress(newEmail.value, currentPassword.value);"> Save </button>
</div>
帐户组件 TS
saveNewEmailAddress(email: string, password: string) {
this.authService.updateEmailAddress(email, password);
}
身份验证服务
updateEmailAddress(email: string, password: string) {
const currentUser = firebase.auth().currentUser;
const credentials = firebase.auth.EmailAuthProvider.credential(currentUser.email, password);
currentUser.reauthenticateWithCredential(credentials).then(function () {
currentUser.updateEmail(email).then(function () {
console.log('i think it worked!');
currentUser.sendEmailVerification().then(function () {
console.log('email sent');
}).catch(function (error) {
// An error happened.
});
}).catch(function (error) {
console.log(error);
});
})
}
【问题讨论】:
标签: angular firebase firebase-authentication