angular - 使用 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(){
}
}
解决方案
当你想访问时,新的 API 返回一个 observable currentUser
。所以我这样做了:
async SendVerificationMail() {
(await this.afAuth.currentUser).sendEmailVerification().then(() => {
console.log('email sent');
});
}
推荐阅读
- codeigniter-3 - 严重性:通知消息:未定义变量:数据文件名:frontend/v_grafik.php 行号:17
- react-native - 当想要在原生平台的应用商店上发布 react-native 应用时,澄清部署工作流程?
- javascript - 如何为我的自定义库组件正确加载字体系列?
- eclipse - 在我的项目中使用 Jsoup 库时遇到问题
- django - 有没有办法将过滤器应用于使用 Django 中的 URL 标签生成的 URL?
- django - 'str' 对象没有使用 Django Rest 框架的属性 'items'
- javascript - 数组拆分
- haskell - 伊德里斯有 MaybeT 吗?
- php - for 循环上的 DateTime->modify('+1 day') 更改另一个变量
- kendo-ui - 在剑道 ui 网格中获取 id