使用 firebase v9 发送验证邮件
Send verification email with firebase v9
我在使用 firebase 身份验证模块发送验证电子邮件时遇到问题。在以前的 firebase 版本中,我曾经这样做过:
await firebase.auth().createUserWithEmailAndPassword(data.email, data.password);
await firebase.auth().currentUser?.sendEmailVerification();
对于新版本的 firebase,我需要这样做:
import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';
...
const auth = getAuth();
await createUserWithEmailAndPassword(auth, email, password);
await sendEmailVerification()
问题是 Typescript 强制我将一个 User 类型的参数传递给 sendEmailVerification,我不知道我应该从哪里得到这个用户参数。我尝试从 createUserWithEmailAndPassword 返回一些东西,但 Typescript 说它是错误的类型。所以我的问题是如何获取这个用户对象?
await createUserWithEmailAndPassword(auth, email, password).then((cred) => await sendEmailVerification(cred.user))
await createUserWithEmailAndPassword(auth, email, password).then((cred) => await sendEmailVerification(cred))
其中任何一个都应该有效。
您需要传递存在于 auth
中的 currentUser
,类似下面的内容应该有效:
import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';
...
const auth = getAuth();
await createUserWithEmailAndPassword(auth, email, password);
await sendEmailVerification(auth.currentUser)
要使用 Firebase v9
发送验证电子邮件,您可以使用 sendEmailVerification()
方法。
要使用它,您只需使用下面的代码即可。一旦 he/she 创建了一个帐户,它将向用户发送一封验证邮件。
注意:在下面的代码中,auth
、email
和 password
假设它们是 !undefined
。
await createUserWithEmailAndPassword(auth, email, password)
.then((cred) => await sendEmailVerification(cred.user));
在 运行 上面的代码(使用 createUserWithEmailAndPassword
方法)之后,它应该向用户发送一封验证邮件。
有关详细信息,请访问 official Firebase v9
documentation 关于发送电子邮件验证。
我在使用 firebase 身份验证模块发送验证电子邮件时遇到问题。在以前的 firebase 版本中,我曾经这样做过:
await firebase.auth().createUserWithEmailAndPassword(data.email, data.password);
await firebase.auth().currentUser?.sendEmailVerification();
对于新版本的 firebase,我需要这样做:
import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';
...
const auth = getAuth();
await createUserWithEmailAndPassword(auth, email, password);
await sendEmailVerification()
问题是 Typescript 强制我将一个 User 类型的参数传递给 sendEmailVerification,我不知道我应该从哪里得到这个用户参数。我尝试从 createUserWithEmailAndPassword 返回一些东西,但 Typescript 说它是错误的类型。所以我的问题是如何获取这个用户对象?
await createUserWithEmailAndPassword(auth, email, password).then((cred) => await sendEmailVerification(cred.user))
await createUserWithEmailAndPassword(auth, email, password).then((cred) => await sendEmailVerification(cred))
其中任何一个都应该有效。
您需要传递存在于 auth
中的 currentUser
,类似下面的内容应该有效:
import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';
...
const auth = getAuth();
await createUserWithEmailAndPassword(auth, email, password);
await sendEmailVerification(auth.currentUser)
要使用 Firebase v9
发送验证电子邮件,您可以使用 sendEmailVerification()
方法。
要使用它,您只需使用下面的代码即可。一旦 he/she 创建了一个帐户,它将向用户发送一封验证邮件。
注意:在下面的代码中,auth
、email
和 password
假设它们是 !undefined
。
await createUserWithEmailAndPassword(auth, email, password)
.then((cred) => await sendEmailVerification(cred.user));
在 运行 上面的代码(使用 createUserWithEmailAndPassword
方法)之后,它应该向用户发送一封验证邮件。
有关详细信息,请访问 official Firebase v9
documentation 关于发送电子邮件验证。