Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Email at the time of transaction with nodemailer OAuth2. Fixes #46 - Task 2 (Complete) #55

Merged
merged 11 commits into from
Aug 24, 2020
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