Last active
August 24, 2021 02:00
-
-
Save alan89/9544c192de363dc4c2396dadf590f258 to your computer and use it in GitHub Desktop.
This is a firebase cloud function example that sends a custom action email through Sendgrid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
const sgMail = require('@sendgrid/mail'); | |
// Using @sendgrid/mail | |
// TODO: Configure the `sendgrid.key` and `sendgrid.template` Google Cloud environment variables. | |
const API_KEY = functions.config().sendgrid.key; | |
const TEMPLATE_ID = functions.config().sendgrid.template; | |
sgMail.setApiKey(API_KEY); | |
// Sends an email confirmation when a user is created. | |
exports.sendVerificationEmail = functions | |
.auth | |
.user() | |
.onCreate((user) => { | |
admin.auth().generateEmailVerificationLink(user.email) | |
.then(async (link) => { | |
// Construct email verification template, embed the link and send | |
// using Sendgrid. | |
const msg = { | |
to: user.email, | |
from: sender.email, | |
templateId: TEMPLATE_ID, | |
dynamic_template_data: { | |
confirmationLink: link, | |
username: user.displayName, | |
}, | |
}; | |
return sgMail.send(msg); | |
}) | |
.catch((error) => { | |
console.error(error); | |
return '200'; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment