Home > OS >  Send verification email with firebase v9
Send verification email with firebase v9

Time:03-23

I have a problem with sendiing a verification email with firebase authentication module. In previous version of firebase I used to do it like this:

 await firebase.auth().createUserWithEmailAndPassword(data.email, data.password);

 await firebase.auth().currentUser?.sendEmailVerification();

And with new version of firebase I need to do it like this:

import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';
...
const auth = getAuth();

await createUserWithEmailAndPassword(auth, email, password);

await sendEmailVerification()

Problem is that Typescript forces me to pass a parameter of type User to sendEmailVerification, and I don't know where should i get this user parameter from. I tried returning something from createUserWithEmailAndPassword but Typescript says that it's a wrong type. So my question is how to get this user object?

CodePudding user response:

you need to pass the currentUser that exists within auth, something like below should work:

import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';
...
const auth = getAuth();

await createUserWithEmailAndPassword(auth, email, password);

await sendEmailVerification(auth.currentUser)

CodePudding user response:

await createUserWithEmailAndPassword(auth, email, password).then((cred) => await sendEmailVerification(cred.user))

await createUserWithEmailAndPassword(auth, email, password).then((cred) => await sendEmailVerification(cred))

Either one of these should work.

CodePudding user response:

To send a verification email with Firebase v9, you can use the sendEmailVerification() method.

To use it, you can just use the code below. It will send the user a verification email once he/she has created an account.

Note: With the code below, auth, email, and password are assuming that they are !undefined.

await createUserWithEmailAndPassword(auth, email, password)
  .then((cred) => await sendEmailVerification(cred.user));

After running the code above (with the createUserWithEmailAndPassword method), it should send the user a verification email.


For more information, visit the official Firebase v9 documentation about sending email verifications.

  • Related