DEV Community

Cover image for Node.js and Firebase: Generating Email Verification Link
Francisco Inoque
Francisco Inoque

Posted on

Node.js and Firebase: Generating Email Verification Link

Hi guys,

In this API I will explain how to generate a link to verify email from using firebase-admin and send this link to the user using nodemailer. Let's start now.

  • 1 let's create the email-verification folder, inside the folder we will open the terminal and run the following commands:

yarn init -y To start a Nodejs project.

yarn add firebase-admin express nodemail

yarn add nodemon typescript -D

require('dotenv').config()
import admin from 'firebase-admin';




import { serviceAccount } from './service'

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: process.env.DATABASEURL

});





  export const adminAuth = admin.auth()

Enter fullscreen mode Exit fullscreen mode
  • 3 Third, we will create the sendEmail.service.ts file which is the body of the message that will be sent to the user:
import nodemailer from 'nodemailer'
import { adminAuth } from '../config/firebase';


const transporter = nodemailer.createTransport({
  host: "Your hsot",
  port: 3333,
  auth: {
    user: "username",
    pass: "password"
  }
})

export const emailVerification = async  (req, res) => {
        const { email } = req.body;
        const first_name = "Francisco"



        try {
            adminAuth.generateEmailVerificationLink(email)
            .then(async(emailLink) => {

                const uid = await (await adminAuth.getUserByEmail(email)).uid;





                await  await transporter.sendMail({
                  from: "Francisco Inoque <accounts@franciscoinoque.tech>",
                  to: email,
                  subject: "Email Verification",
                  html: `Hello ${first_name}, to verify your email please, <a href="${emailLink}"> click here </a>`
                })

                return await res.json({success_msg: "please check in your inbox, we sent verification email"})

            }).catch(error => {
                return res.json(error)
            })
        } catch (error) {
            return res.json(error)  
        }


    }



Enter fullscreen mode Exit fullscreen mode
  • Fourth, let's create the router.ts file:
import { Router } from 'express'
import { emailVerification } from '../services/emailVerification.service';

export const router = Router();

router.post('/send-email-verification',emailVerification)
Enter fullscreen mode Exit fullscreen mode
  • And finally we're going to create the server.ts file:
import { config } from 'dotenv'
config()
import express from 'express'

import {router} from './routes/router'



const app = express()

app.use(express.json())
app.use('/api', router)



const port = process.env.PORT || 3033;
app.listen(port,  () => console.log(`Server is running on http://localhost:${port}`));

Enter fullscreen mode Exit fullscreen mode

Thanks, please clone the full repository here and give me a star

Top comments (0)