[SOLVED] Send verification email with firebase v9

Issue

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?

Solution

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)

Answered By – mocherfaoui

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *