【问题标题】:Send Email Verification with Firebase using AngularFire v6使用 AngularFire v6 使用 Firebase 发送电子邮件验证
【发布时间】:2020-10-10 23:53:26
【问题描述】:

我正在尝试使用电子邮件和密码注册用户。如果注册功能有效,则会向用户的电子邮件地址发送一封邮件,该邮件必须包含一个链接,用户将单击该链接以确认电子邮件地址。我尝试使用 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(){

  }
}

【问题讨论】:

    标签: angular firebase-authentication angularfire email-verification ionic5


    【解决方案1】:

    我找到了。只需在您登录用户后使用此功能。如果没有用户登录,请不要使用它。

    firebase.auth().currentUser.sendEmailVerification()
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        此解决方案适用于 Angular 11,没有任何编译错误/黑客攻击。

        在注册/登录后使用电子邮件身份验证从结果中获取用户对象,然后使用它发送验证电子邮件

        signUp(email: string, password: string) {
        this.afAuth.createUserWithEmailAndPassword(email, password)
          .then((result) => {
        
            /** sends verification email **/
            result.user.sendEmailVerification();
        
          }).catch((error) => {
            window.alert(error.message)
          });
        

        }

        【讨论】:

          猜你喜欢
          • 2017-03-17
          • 2019-03-13
          • 2020-09-08
          • 2020-08-06
          • 2019-06-24
          • 2018-11-21
          • 1970-01-01
          • 1970-01-01
          • 2017-09-09
          相关资源
          最近更新 更多