首页 > 解决方案 > 使用 AngularFire v6 使用 Firebase 发送电子邮件验证

问题描述

我正在尝试使用电子邮件和密码注册用户。如果注册功能有效,则会向用户的电子邮件地址发送一封邮件,该邮件必须包含一个链接,用户将单击该链接以确认电子邮件地址。我尝试使用AngularFireAuth.auth.currentUser.sendEmailVerification()函数来执行此操作,但它不起作用:此版本的 AngularFire/Firebase 不支持此方法。你能帮助我吗 ?

import {Injectable, NgZone} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {first} from 'rxjs/internal/operators/first';
import * as firebase from 'firebase';
import {AngularFirestore} from '@angular/fire/firestore';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  public userId: string;

  constructor(
      private afAuth: AngularFireAuth,
      private afStore: AngularFirestore,
      public ngZone: NgZone
  ) { }

  getUser(): Promise<firebase.User>{
    return this.afAuth.authState.pipe(first()).toPromise();
  }
  loginWithEmailAndPassword(email: string, password: string): Promise<firebase.auth.UserCredential>{
    return this.afAuth.signInWithEmailAndPassword(email, password);
  }
  async registerWithEmailAndPassword(email: string, password: string): Promise<firebase.auth.UserCredential>{
    const newUserCredential: firebase.auth.UserCredential = await this.afAuth.createUserWithEmailAndPassword(email, password);
    this.afStore.collection('users').doc(`${newUserCredential.user.uid}`).set({
      uid: newUserCredential.user.uid,
      email: newUserCredential.user.email,
      created_at: firebase.firestore.FieldValue.serverTimestamp()
    }).then(function() {
      console.log("user is registered");
      this.sendEmailVerification();
    }).catch(function(error) {
      console.error("Error adding document: ", error);
    });
    return newUserCredential;
  }

  async sendEmailVerification() {
    
  }

  resetPassword(email: string): Promise<void>{
    return this.afAuth.sendPasswordResetEmail(email);
  }

  logout(): Promise<void>{
    return this.afAuth.signOut();
  }

  async createPersonalProfile(){

  }
}

标签: angularfirebase-authenticationangularfireemail-verificationionic5

解决方案


当你想访问时,新的 API 返回一个 observable currentUser。所以我这样做了:

async SendVerificationMail() {
    (await this.afAuth.currentUser).sendEmailVerification().then(() => {
        console.log('email sent');
    });
}

推荐阅读