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?
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With