Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const accountVerificationTemplate = (url) => ({ | ||
subject: `🎉 Successfully created a new account on Fakebook`, | ||
bodyHtml: ` | ||
<h1>Thanks for helping me with the study 😎</h1> | ||
<a href="${url}">Verify your account by clicking here </a> | ||
` | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Mailer service to handle email sending | ||
* @module service/mailer | ||
*/ | ||
|
||
import nodemailer from "nodemailer"; | ||
|
||
/** | ||
* Sends an email | ||
* @param {Array} recipients email addresses of recipients | ||
* @param {String} subject email subject | ||
* @param {String} bodyHtml email body | ||
* @returns {Object} status of the email | ||
*/ | ||
const mailTo = async (recipients, { subject, bodyHtml }) => { | ||
const transporter = nodemailer.createTransport({ | ||
host: "mail.privateemail.com", | ||
port: 465, | ||
auth: { | ||
user: process.env.MAILER_USER, | ||
pass: process.env.MAILER_PASSWORD, | ||
}, | ||
}); | ||
const data = { | ||
from: process.env.MAILER_USER, | ||
to: recipients, | ||
subject: subject, | ||
html: bodyHtml, | ||
}; | ||
return await sendEmail(transporter, data); | ||
}; | ||
|
||
/** | ||
* Wrapper for sending emails that returns a promise | ||
* @param {Object} transporter Nodemailer transporter object | ||
* @param {Object} data data to be send | ||
*/ | ||
const sendEmail = async (transporter, data) => | ||
new Promise((resolve, reject) => { | ||
transporter.sendMail(data, (err, info) => { | ||
if (err) reject(err); | ||
resolve(info); | ||
}); | ||
}); | ||
|
||
export default mailTo; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import mongoose from "../helpers/db.js"; | ||
|
||
const AccountVerificationCode = mongoose.model( | ||
"AccountVerificationCode", | ||
new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
}, | ||
code: { | ||
type: String, | ||
required: true, | ||
}, | ||
dateCreated: { | ||
type: Date, | ||
default: Date.now(), | ||
expires: 600, | ||
}, | ||
}) | ||
); | ||
|
||
export default AccountVerificationCode; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -34,6 +34,10 @@ const User = mongoose.model( | ||
}, | ||
userAgent: { | ||
type: Object | ||
}, | ||
status: { | ||
type: String, | ||
default: 'pending' | ||
} | ||
}) | ||
); | ||
Some generated files are not rendered by default. Learn more.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import express from "express"; | ||
// import cfg from "config"; | ||
import User from "../models/user.js"; | ||
import AccountVerificationCode from "../models/accountVerificationCode.js"; | ||
|
||
const mode = process.env.NODE_ENV || "dev"; | ||
// const config = cfg.get(mode); | ||
const router = express.Router(); | ||
|
||
router.post("/verify-account", async (req, res) => { | ||
const { email, secretCode } = req.body; | ||
try { | ||
const verificationCode = await AccountVerificationCode.findOne({ code: secretCode }); | ||
const user = await User.findOne({ email: email }); | ||
if ((user && verificationCode) && verificationCode.email === user.email) { | ||
await User.findOneAndUpdate({ email: email }, { status: "active" }); | ||
return res.status(200).json({ status: "Success" }); | ||
} else { | ||
return res.status(401).json({ error: "Incorrect verification code" }); | ||
} | ||
} catch (err) { | ||
return res.status(500).json({ error: "Server error: " + err }); | ||
} | ||
}); | ||
|
||
export default router; |
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