Node.js and Firebase: Generating Email Verification Link
Tags: javascript, node, firebase, typescript
Published: 2021-04-11 22:43:56

### 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`

* [To configure typescript please visit the project repository](https://github.com/frantchessico/generating-link-to-verify-email-with-firebase-admin)

* 2 Second, let's create a firebase.ts file
``` js
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()
  
```
* 3 Third, we will create the sendEmail.service.ts file which is the body of the message that will be sent to the user:

```js
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)  
        }
         
        
    }
    
    

```

* Fourth, let's create the router.ts file:

```js
import { Router } from 'express'
import { emailVerification } from '../services/emailVerification.service';

export const router = Router();

router.post('/send-email-verification',emailVerification)
```


* And finally we're going to create the server.ts file:


```js
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}`));

```


# Thanks, please clone the full repository [here](https://github.com/frantchessico/generating-link-to-verify-email-with-firebase-admin) and give me a star