Skip to content

Commit dd0efb3

Browse files
Merge pull request #108 from MassiGy/master
npm uninstall cookie
2 parents d5da9f1 + 8cc750f commit dd0efb3

File tree

7 files changed

+69
-46
lines changed

7 files changed

+69
-46
lines changed

package-lock.json

-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"license": "MIT",
2121
"dependencies": {
2222
"bcrypt": "^5.0.0",
23-
"cookie": "^0.4.2",
2423
"cookie-parser": "^1.4.6",
2524
"cors": "^2.8.5",
2625
"dotenv": "^10.0.0",

src/helpers/mailer.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const nodemailer = require("nodemailer");
2+
3+
const transporter = nodemailer.createTransport({
4+
service: 'gmail',
5+
auth: {
6+
7+
pass: process.env.GMAIL_APP_PASS,
8+
},
9+
});
10+
11+
12+
/**
13+
* Mailer is an abstraction layer that allows us to send emails using node-mailer under the hood.
14+
* @param {String} mailMarkup - html based markup
15+
* @param {String} receivers - a comma sperated list of email addresses
16+
* @param {String} mailSubject - the mail subject
17+
*/
18+
19+
module.exports.mailer = function(mailMarkup, receivers, mailSubject) {
20+
transporter.sendMail({
21+
from: '"Fairfield Programming Association" <[email protected]>', // sender address
22+
to: receivers, // list of receivers
23+
subject: mailSubject, // Subject line
24+
html: mailMarkup,
25+
});
26+
}

src/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ app.use(express.json());
1717
app.use(require("cors")({ origin: "https://fairfieldprogramming.org" }));
1818

1919

20-
20+
/**
21+
* Verfies if the user has a confimed email address, otherwise send an error message
22+
* @param {Request} req HTTP Request
23+
* @param {Response} res HTTP Response
24+
* @param {Middelware} next calls the next middelware
25+
* @returns {Response}
26+
*/
2127

2228
const verifyEmail = async (req, res, next) => {
2329
try {
@@ -30,6 +36,13 @@ const verifyEmail = async (req, res, next) => {
3036
}
3137
}
3238

39+
/**
40+
* Verfies if the user is logged in, otherwise redirect to "/login"
41+
* @param {Request} req HTTP Request
42+
* @param {Response} res HTTP Response
43+
* @param {Middelware} next calls the next middelware
44+
* @returns {Response}
45+
*/
3346

3447
const verifyLogin = (req, res, next) => {
3548
if (req.cookies.token) {
@@ -84,8 +97,8 @@ app.post('/user/:id/block/:blockId/block', verifyLogin, verifyEmail, require('./
8497
app.post('/user/:id/block/:blockId/undo', verifyLogin, verifyEmail, require('./routes/User/Block/unblockUser'));
8598

8699
// Follow Endpoints
87-
app.get('/user/:id/followers', require('./routes/User/Followers/listFollowers'));
88-
app.get('/user/:id/followers/:followerId', require('./routes/User/Followers/queryFollower'));
100+
app.get('/user/:id/followers', verifyLogin, require('./routes/User/Followers/listFollowers'));
101+
app.get('/user/:id/followers/:followerId', verifyLogin, require('./routes/User/Followers/queryFollower'));
89102

90103
app.post('/user/:id/followers/:followerId/follow', verifyLogin, verifyEmail, require('./routes/User/Followers/followUser'));
91104
app.post('/user/:id/followers/:followerId/undo', verifyLogin, verifyEmail, require('./routes/User/Followers/unfollowUser'));

src/routes/User/Account/login.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* @module login
3+
* HTTP POST Request on "/login" handler
4+
* @param {Request} req - HTTP POST Request on "/login"
5+
* @param {Response} res - HTTP Response
6+
* @returns {Response} HTTP Response
7+
* @description This route handler will listen to the client request,
8+
* check if all parameter are good, look if there is a user in the data base with those credentials
9+
* then if all goes well, send back a cookie to the client.
10+
*/
11+
112
const { compare } = require("bcrypt");
213
const { sign } = require("jsonwebtoken");
314

@@ -23,7 +34,6 @@ module.exports = (req, res) => {
2334
if (!result) return res.status(403).send("Invalid Credentials.");
2435

2536
if (err) {
26-
console.log(err);
2737
return res.status(500).send("Internal Server Error.");
2838
}
2939

@@ -43,7 +53,6 @@ module.exports = (req, res) => {
4353
);
4454
})
4555
.catch((error) => {
46-
console.log(error);
4756
return res.status(500).send("Internal Server Error.");
4857
});
4958
};

src/routes/User/Account/signup.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const fs = require("fs");
22
const path = require("path");
33
const { hash } = require("bcrypt");
44
const { sign } = require("jsonwebtoken");
5-
const cookie = require("cookie");
6-
const nodemailer = require("nodemailer");
5+
const { mailer } = require("../../../helpers/mailer");
76
const
87
{
98
invalidPassword,
@@ -58,35 +57,26 @@ module.exports = async (req, res) => {
5857
);
5958

6059
const id_token = sign({ id: data.id }, process.env.EMAIL_TOKEN, { expiresIn: "4 days", });
61-
let transporter = nodemailer.createTransport({
62-
service: 'gmail',
63-
auth: {
64-
65-
pass: process.env.GMAIL_APP_PASS,
66-
},
67-
});
60+
6861

6962
let emailData = fs.readFileSync(path.join(process.cwd(), "/res/emails/confirmEmail.html"), 'ascii');
7063

71-
emailData = emailData.replace("${data.username}", data.email);
64+
emailData = emailData.replace("${data.username}", data.username);
7265
emailData = emailData.replace("${id_token}", id_token);
7366

74-
// send mail with defined transport object
75-
transporter.sendMail({
76-
from: '"Fairfield Programming Association" <[email protected]>', // sender address
77-
to: `${data.email}`, // list of receivers
78-
subject: "Confirm Your Email Address", // Subject line
79-
html: emailData,
80-
});
67+
// send the email
68+
mailer(emailData, String(data.email), "Confirm Your Email Address")
8169

82-
res.json({ token: sign(
83-
{
84-
id: data.id,
85-
username: data.username,
86-
email: data.email,
87-
},
88-
process.env.JWT_KEY,
89-
) });
70+
res.json({
71+
token: sign(
72+
{
73+
id: data.id,
74+
username: data.username,
75+
email: data.email,
76+
},
77+
process.env.JWT_KEY,
78+
)
79+
});
9080

9181
}
9282
)

src/routes/User/Followers/listFollowers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = (req, res) => {
22
if (!req.user) return res.status(403).send("Not Logged In.");
3-
if (!req.params.id || !req.params.followerId) return res.status(400).send("Not All Parameters Provided.");
3+
if (!req.params.id) return res.status(400).send("Not All Parameters Provided.");
44

55
User.findOne(
66
{

0 commit comments

Comments
 (0)