Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase send email verification to user

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 ?

like image 430
Phil Avatar asked Apr 23 '20 14:04

Phil


People also ask

Can Firebase send emails?

Made by Firebase Composes and sends an email based on the contents of a document written to a specified Cloud Firestore collection.

How do I create a verification email link?

To generate an email verification link, provide the existing user's unverified email and an optional ActionCodeSettings object. The operation will resolve with the email action link. The email used must belong to an existing user. // Admin SDK API to generate the email verification link.

Does Firebase charge for email verification?

So you don't pay for verifying an email address, nor for each API call you make, but only for completed verifications through the code that was sent in the SMS message.

How do you verify email addresses in Firebase?

Firebase offers a handy tool for that called email verification link, and it's a link you send to your users' emails, they can click it, and it will verify their emails inside of Firebase Authentication. Then you can decide to only give access to verified users, or run a cleaning tool to remove non-verified users after set period of time.

How do I use Firebase authentication to sign in?

You can use Firebase Authentication to sign in a user by sending them an email containing a link, which they can click to sign in. In the process, the user's email address is also verified. There are numerous benefits to signing in by email: Low friction sign-up and sign-in.

How to send email verification link with Firebase using ReactJS?

How to send email verification link with firebase using ReactJS? 1 Step 1: Create a React myapp using the following command.#N#npx create-react-app myapp 2 Step 2: After creating your project folder i.e. myapp, move to it using the following command.#N#cd myapp More ...

How to initialize firebase in your project?

Example: Initialize the firebase into your project by creating firebase.js file with the following code. Now write some code into your App.js file. Here, When you click on sign-up button the verification email is send to the provided email address. Here is the verification mail.


2 Answers

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.

like image 102
Renaud Tarnec Avatar answered Sep 29 '22 07:09

Renaud Tarnec


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.

like image 24
Frank van Puffelen Avatar answered Sep 29 '22 08:09

Frank van Puffelen