【问题标题】:How re-authenticate user with email and password?如何使用电子邮件和密码重新验证用户身份?
【发布时间】:2019-03-27 10:34:04
【问题描述】:

我想为记住密码的用户更改密码。我有用户在其中写入数据的电子邮件和密码字段,如果它们是正确的,他将看到新的输入字段,他可以在其中输入新密码。我阅读了有关重新身份验证的官方 Firebase 文档,但不明白我如何使用它。

这是我放置电子邮件输入和密码输入的组件

import {Component, Inject, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
import {DialogDataInterface} from '../Interfaces/DialogDataInterface';

@Component({
  selector: 'app-pop-up-dialog',
  templateUrl: './pop-up-dialog.component.html',
  styleUrls: ['./pop-up-dialog.component.css']
})

export class PopUpDialogComponent implements OnInit {
  private email: string;
  private password: string;
  public resetPassword: FormGroup;

  constructor( private fb: FormBuilder,
               public dialogRef: MatDialogRef<PopUpDialogComponent>,
               @Inject(MAT_DIALOG_DATA) public data: DialogDataInterface) { }

  ngOnInit() {
    this.resetPassword = this.fb.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', [Validators.required]]
    });
    this.resetPassword.valueChanges.subscribe(data => {
      this.email = data.email;
      this.password = data.password;
    })
  }

  onCloseClick(): void{
    this.dialogRef.close();
  }

  onOkClick(){
    //Here i must check re-auth user
  }
}

我的用户服务:

import {Injectable} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {AngularFirestore, AngularFirestoreCollection} from '@angular/fire/firestore';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {Router} from '@angular/router';
import {UserSignUpInterface} from './userSignUpInterface';
import {UserInterface} from './userInterface';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private UserCollectionName = 'User';
  private UserCollection: AngularFirestoreCollection = this.afs.collection(this.UserCollectionName);
  constructor(public afAuth: AngularFireAuth,
              private afs: AngularFirestore,
              private router: Router) {}
  logIn(email, pass) {
    this.afAuth.auth.signInWithEmailAndPassword(email, pass)
      .then(user => {
        this.router.navigate(['/toDoList']);
      }).catch(err => console.log(err));
  }
  logOut(): void {
    this.afAuth.auth.signOut().then(user => {
      localStorage.removeItem('user');
      this.router.navigate(['/auth']);
      console.log('you log out');
    })
      .catch(err => console.log(err));
  }
  signUp(email, pass, objUser: UserSignUpInterface) {
    return this.afAuth.auth.createUserWithEmailAndPassword(email, pass)
      .then(user => {
        this.UserCollection.doc(user.user.uid).set(objUser);
        this.router.navigate(['./auth']);
      });
  }
  sendResetPassEmail(email){
    return  this.afAuth.auth.sendPasswordResetEmail(email)
  }
  reAuthUser(email, password){
    // const user = this.afAuth.auth.currentUser;
    // const credintial = this.afAuth
    // this.afAuth.auth.currentUser.reauthenticateAndRetrieveDataWithCredential(credintial))
  }
}

【问题讨论】:

  • 在使用 FIrebase 库方面您尝试过什么?
  • 使用电子邮件和密码验证用户,注销用户,将数据存储在 Firestore 中并获取此数据
  • 我的意思是举个例子。您应该扩展您的代码示例,以包括您尝试使用 Firebase 的方式并添加您迄今为止尝试过的内容。
  • 我用用户服务更新问题。在这里我为用户提供的所有功能。

标签: javascript firebase firebase-authentication


【解决方案1】:

在我的情况下,这很好,你可以试试这个:

onOkClick(){

    var user = firebase.auth().currentUser;

       user.updatePassword(this.password).then(function() {
          console.log("successfully added!");
       }).catch(function(error) {
          console.log("unsuccessfull");
      });

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2019-06-11
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多