Skip to content

Commit

Permalink
Merge pull request #55 from fabcodingzest/fix/#46
Browse files Browse the repository at this point in the history
Send Email at the time of transaction with nodemailer OAuth2. Fixes #46 - Task 2 (Complete)
  • Loading branch information
iampavangandhi authored Aug 24, 2020
2 parents 11bdf9e + 32ae0ad commit 796c08d
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 28 deletions.
16 changes: 8 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo")(session);
const connectDB = require("./config/db");
const flash = require('connect-flash');
const flash = require("connect-flash");

// Load config
dotenv.config({ path: "./config/config.env" });
Expand Down Expand Up @@ -78,15 +78,15 @@ app.use(passport.initialize());
app.use(passport.session());

// Connect Flash
app.use(flash())
app.use(flash());

// Set Global variables
app.use(function(req, res, next) {
res.locals.user = req.user || null;
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
app.use(function (req, res, next) {
res.locals.user = req.user || null;
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});

// Static folder
Expand Down
30 changes: 30 additions & 0 deletions helpers/emailHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require("dotenv").config();
const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: process.env.GMAIL_ADDRESS,
clientId: process.env.GMAIL_OAUTH_CLIENT_ID,
clientSecret: process.env.GMAIL_OAUTH_CLIENT_SECRET,
refreshToken: process.env.GMAIL_OAUTH_REFRESH_TOKEN,
accessToken: process.env.GMAIL_OAUTH_ACCESS_TOKEN,
expires: Number.parseInt(process.env.GMAIL_OAUTH_TOKEN_EXPIRE, 10),
},
});

exports.sendEmail = (mailOptions) =>
new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error) => {
if (error) {
console.error(error.stack || error);
return reject(error);
}
resolve();
});
}).catch((error) => {
console.log(error);
});
1 change: 0 additions & 1 deletion helpers/helper.js

This file was deleted.

Loading

0 comments on commit 796c08d

Please sign in to comment.