如何使用电子邮件和密码重新认证用户?

时间:2019-03-27 10:34:05

标签: javascript firebase firebase-authentication

我想为记住密码的用户更改密码。我有电子邮件和密码字段,用户可以在其中写入数据,如果正确,他将看到新的输入字段,可以在其中输入新密码。我阅读了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
  }
}

我的UserService:

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))
  }
}

1 个答案:

答案 0 :(得分:0)

就我而言,这很好,您可以尝试以下方法:

onOkClick(){

    var user = firebase.auth().currentUser;

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