Skip to content

Instantly share code, notes, and snippets.

@doug-orchard
Created September 26, 2019 13:19
Show Gist options
  • Save doug-orchard/247c7234b74ab428a01986b7303ff3dd to your computer and use it in GitHub Desktop.
Save doug-orchard/247c7234b74ab428a01986b7303ff3dd to your computer and use it in GitHub Desktop.
Firebase functions - create new user in db when new user is added to auth. and send mail
'use strict';
const admin = require('firebase-admin');
const functions = require("firebase-functions");
const nodemailer = require('nodemailer');
admin.initializeApp();
const db = admin.firestore();
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("<h1>Hello from Firebase!</h1>");
});
const gmailEmail = "email@gmail.com";
const gmailPassword = "password";
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
exports.sendWelcomeEmail = functions.auth.user().onCreate(user => {
console.log("user is...: ", user );
console.log("email is...: ", user.email );
const mailOptions = {
from: `"${user.uid}" <email@gmail.com>`,
to: "email@gmail.com",
subject: `Welcome ${user.uid}`,
html: `Welcome ${user.uid}`
};
var userObject = {
displayName : "Name",
email : user.email,
createdOn : user.metadata.creationTime
};
// this is working so dont change it!!
try {
mailTransport.sendMail(mailOptions);
console.log(`mail sent`);
} catch(error) {
console.error('There was an error while sending the email:', error);
}
return db.doc(`users/${user.uid}`).set(userObject);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment