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

问题描述 投票:0回答:1

我想为记住密码的用户更改密码。我有电子邮件和密码字段,用户在其中写入数据,如果正确,他将看到新的输入字段,他可以在其中输入新密码。我阅读了有关重新身份验证的官方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))
  }
}
javascript firebase firebase-authentication
1个回答
0
投票

在我的情况下,这工作正常,你可以试试这个:

onOkClick(){

    var user = firebase.auth().currentUser;

       user.updatePassword(this.password).then(function() {
          console.log("successfully added!");
       }).catch(function(error) {
          console.log("unsuccessfull");
      });
© www.soinside.com 2019 - 2024. All rights reserved.