0
0
FirebaseHow-ToBeginner · 3 min read

How to Send Verification Email with Firebase Authentication

To send a verification email in Firebase, use the sendEmailVerification() method on the currently signed-in user object. This triggers Firebase to send an email with a verification link to the user's registered email address.
📐

Syntax

The sendEmailVerification() method is called on the current user object obtained from Firebase Authentication. It returns a promise that resolves when the email is sent successfully.

  • firebase.auth().currentUser: The signed-in user object.
  • sendEmailVerification(): Sends the verification email.
  • Returns a Promise to handle success or failure.
javascript
firebase.auth().currentUser.sendEmailVerification()
  .then(() => {
    // Email sent successfully
  })
  .catch(error => {
    // Handle error
  });
💻

Example

This example shows how to send a verification email after a user signs up with email and password. It handles success and error cases.

javascript
import { initializeApp } from 'firebase/app';
import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth';

const firebaseConfig = {
  apiKey: 'YOUR_API_KEY',
  authDomain: 'YOUR_AUTH_DOMAIN',
  projectId: 'YOUR_PROJECT_ID',
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

async function signUpAndSendVerification(email, password) {
  try {
    const userCredential = await createUserWithEmailAndPassword(auth, email, password);
    const user = userCredential.user;
    await sendEmailVerification(user);
    console.log('Verification email sent to:', user.email);
  } catch (error) {
    console.error('Error during sign up or sending email:', error.message);
  }
}

// Example usage:
signUpAndSendVerification('user@example.com', 'securePassword123');
Output
Verification email sent to: user@example.com
⚠️

Common Pitfalls

  • Calling sendEmailVerification() before the user is signed in will fail because currentUser is null.
  • Not handling promise rejections can hide errors like network issues or invalid user state.
  • For new users, ensure you wait for the user creation to complete before sending the verification email.
  • Remember to check if the user's email is already verified to avoid sending unnecessary emails.
javascript
/* Wrong: calling sendEmailVerification without signed-in user */
firebase.auth().sendEmailVerification() // This will cause an error

/* Right: call on currentUser after sign-in */
firebase.auth().currentUser.sendEmailVerification()
  .then(() => console.log('Email sent'))
  .catch(error => console.error(error));
📊

Quick Reference

Tips for sending verification emails with Firebase Authentication:

  • Use sendEmailVerification(user) from the Firebase Auth SDK.
  • Always ensure the user is signed in before sending the email.
  • Handle promise results to confirm success or catch errors.
  • Check user.emailVerified to know if verification is needed.

Key Takeaways

Use sendEmailVerification() on the signed-in user to send a verification email.
Always wait for user sign-in or creation before sending the verification email.
Handle promise success and errors to manage email sending status.
Check if the user's email is already verified to avoid redundant emails.
Use Firebase Auth SDK's latest modular methods for best practice.