How to send email verification after user creation with Firebase Cloud functions?

后端 未结 5 919
遇见更好的自我
遇见更好的自我 2020-12-18 06:46

I\'m trying to send the verification email after the user is created. Since there\'s no way on Firebase itself, I\'m trying it with cloud functions.

I cannot really

5条回答
  •  难免孤独
    2020-12-18 07:07

    This is how I implemented it successfully using Firebase cloud functions along with a small express backend server

    Firebase Cloud function (background) triggered with every new user created

    • This function sends a "user" object to your api endpoint

    const functions = require('firebase-functions');
    const fetch = require('node-fetch');
    
    // Send email verification through express server
    exports.sendVerificationEmail = functions.auth.user().onCreate((user) => {
      // Example of API ENPOINT URL 'https://mybackendapi.com/api/verifyemail/'
      return fetch( < API ENDPOINT URL > , {
          method: 'POST',
          body: JSON.stringify({
            user: user
          }),
          headers: {
            "Content-Type": "application/json"
          }
        }).then(res => console.log(res))
        .catch(err => console.log(err));
    });

    Server Middleware code

    • verifyEmail here is used as middleware

    // File name 'middleware.js'
    import firebase from 'firebase';
    import admin from 'firebase-admin';
    
    // Get Service account file from firebase console
    // Store it locally - make sure not to commit it to GIT
    const serviceAccount = require('');
    // Get if from Firebase console and either use environment variables or copy and paste them directly
    // review security issues for the second approach
    const config = {
      apiKey: process.env.APIKEY,
      authDomain: process.env.AUTHDOMAIN,
      projectId: process.env.PROJECT_ID,
    };
    // Initialize Firebase Admin
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
    });
    
    // Initialize firebase Client
    firebase.initializeApp(config);
    
    export const verifyEmail = async(req, res, next) => {
      const sentUser = req.body.user;
      try {
        const customToken = await admin.auth().createCustomToken(sentUser.uid);
        await firebase.auth().signInWithCustomToken(customToken);
        const mycurrentUser = firebase.auth().currentUser;
        await mycurrentUser.sendEmailVerification();
        res.locals.data = mycurrentUser;
        next();
      } catch (err) {
        next(err);
      }
    };

    Server code

    // Filename 'app.js'
    import express from 'express';
    import bodyParser from 'body-parser';
    // If you don't use cors, the api will reject request if u call it from Cloud functions
    import cors from 'cors';
    import {
      verifyEmail
    } from './middleware'
    
    app.use(cors());
    app.use(bodyParser.urlencoded({
      extended: true,
    }));
    app.use(bodyParser.json());
    
    const app = express();
    // If you use the above example for endpoint then here will be
    // '/api/verifyemail/'
    app.post('', verifyEmail, (req, res, next) => {
      res.json({
        status: 'success',
        data: res.locals.data
      });
      next()
    })

    This endpoint will return back the full user object and will send the verification email to user.

    I hope this helps.

提交回复
热议问题