Das*_*let 3 email-verification angularfire firebase-authentication angular ionic5
我正在尝试使用电子邮件和密码注册用户。如果注册功能有效,则会向用户的电子邮件地址发送一封邮件,该邮件必须包含用户单击以确认电子邮件地址的链接。我尝试使用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(){
}
}
Run Code Online (Sandbox Code Playgroud)
当您想要访问currentUser. 所以我这样做了:
async SendVerificationMail() {
(await this.afAuth.currentUser).sendEmailVerification().then(() => {
console.log('email sent');
});
}
Run Code Online (Sandbox Code Playgroud)
该解决方案适用于 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)
});
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
5543 次 |
| 最近记录: |