Custom verification email on Firebase

Firebase has a function to send verification email to user but this email is uncustomizable and for branding purpose, you might want to combine a welcome mail with the verification process.

For this we create a Firebase Cloud Function (basically a miny node app) which is triggered by the creation of a new user. This function will (1) create a Firebase Auth instance, (2) use it to generate the verification link for the created user and (3) include this link in an email template so you can send it via the email API

var admin = require('firebase-admin')
var fs = require('fs')

var serviceAccount = require('./serviceAccount.json')
var testServiceAccount = require('./testServiceAccount.json')

var defaultApp = admin.initializeApp({
  credential: admin.credential.cert(testServiceAccount),
})

var defaultAuth = defaultApp.auth()

// use node to generate single emails
process.argv.slice(2, process.argv.length).forEach(function(val, index, array) {
  defaultAuth.generateEmailVerificationLink(val).then(res => console.log(res))
})

var output = {}

// Help functions

async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array)
  }
}

// Generating mass links

// var emails = require('./data/emails.json');
// const start = async () => {
//     await asyncForEach(emails, async (email) => {
//         await defaultAuth.generateEmailVerificationLink(email).then( link => {
//             output[email] = link
//             }
//         )
//     })
//     fs.writeFile('output.json', JSON.stringify(output), 'utf8');
// }
// start();