4
votes

I am using Javascript and Vue and integrating firebase with my app

Scenario(Already built)

  • I have a sign in page where users sign in
  • For a user to be signed in the emailVerified property should be true

Problem

  • I am able to send verification email only when i use the firebase.auth().createUserWithEmailAndPassword(email, password) method

Signup method

signup: async (email, password) => {
    const user = await firebase.auth().createUserWithEmailAndPassword(email, password)
    await user.user.sendEmailVerification()
    return `Check your email for verification mail before logging in`
  },

Desired solution

  • I create a new user from the firebase console

  • I pass email as a parameter or uid and that method should send a verification email to the user so they can verify thier email

  • Completely scrap the signup method as i don't need it anymore to send a verification mail

Is there anyway to send a verification email without signing in ?

2

2 Answers

4
votes

Is there anyway to send a verification email without signing in ?

Verification emails can only be sent from the client-side SDK, and only after the user has signed in. This is done to prevent the ability to abuse Firebase's servers for sending spam.

If the existing email verification flow of Firebase doesn't fit your needs, you can implement your own custom flow and use the Admin SDKs to set the the verification status once they've met your verification requirements.

2
votes

You could use a Cloud Function to generate an email verification link, and send it to the user through an email microservice like Sendgrid, Mailjet or Mailgun or via your own custom SMTP server.

You would trigger this Cloud Function when a Firebase user is created using the functions.auth.user().onCreate() event handler.

Since you will create the user through the Firebase console, the Cloud Function will be triggered without the need for the user to sign-in.

Something along these lines:

exports.sendEmailVerification = functions.auth.user().onCreate((user) => {

    const email = user.email;

    const url = '...'  //Optional, see https://firebase.google.com/docs/auth/custom-email-handler
    const actionCodeSettings = {
        url: url
    };

    // Use the Admin SDK to generate the email verification link.
    return admin.auth().generateEmailVerificationLink(email, actionCodeSettings)
        .then((link) => {
            // Construct email verification template, embed the link and send the email
            // by using custom SMTP server.
            // or a microservice like Sendgrid
            return ...
        })
        .catch((error) => {
            // Some error occurred.
        });

});

You will find here an official example of a Cloud Function that sends an email.