Angular2& Firebase - 如何存储电子邮件和密码以使用'reauathenticateWithCredential'

时间:2017-08-02 10:01:53

标签: angular firebase firebase-authentication

我想要实现的目标

  • 一个简单的更新电子邮件和密码'表单'。

问题

  • 当我最近登录时,更改电子邮件正常
  • 当我最近没有通过身份验证时,会抛出错误

问题

  • 如何使用'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);
      });
    })
  }

1 个答案:

答案 0 :(得分:0)

我设法通过在电子邮件更改弹出窗口中包含密码确认来解决此问题。这样我就可以将密码值传入服务,因此重新认证:)