Skip to content

Commit 57ffa2d

Browse files
authored
Merge pull request #177 from hackmcgill/bugfix/172-moreResetPasswordBugs
Fix additional bugs with respect to emails, resetting password
2 parents af9535a + 4a59ea6 commit 57ffa2d

10 files changed

Lines changed: 30 additions & 34 deletions

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"node":true,
55
"this":false,
66
"bad_property":false
7-
}
7+
},
8+
"search.usePCRE2": true
89
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<b>
22
Confirm Account:
3-
<a href="{{link}}">here</a>
3+
<a href="{{{link}}}">here</a>
44
</b>

assets/email/AccountInvitation.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<b>
22
Create Your Account:
3-
<a href="{{link}}">here</a>
3+
<a href="{{{link}}}">here</a>
44
</b>

assets/email/ResetPassword.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<b>
22
Reset password:
3-
<a href="{{link}}">here</a>
3+
<a href="{{{link}}}">here</a>
44
</b>

assets/email/test.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div>This is used for testing email service. DO NOT REMOVE.{{TEST}}</div>
1+
<div>This is used for testing email service. DO NOT REMOVE.{{TEST}}. <a href="{{{NOT_ESCAPED}}}">link</a></div>

middlewares/account.middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async function inviteAccount(req, res, next) {
150150
const confirmationObj = await Services.AccountConfirmation.create(accountType, email);
151151
const confirmationToken = Services.AccountConfirmation.generateToken(confirmationObj.id);
152152

153-
const mailData = Services.AccountConfirmation.generateAccountInvitationEmail(req.hostname, email, accountType, confirmationToken);
153+
const mailData = Services.AccountConfirmation.generateAccountInvitationEmail(process.env.FRONTEND_ADDRESS, email, accountType, confirmationToken);
154154
if (mailData !== undefined) {
155155
Services.Email.send(mailData, (err) => {
156156
if (err) {

middlewares/auth.middleware.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async function sendResetPasswordEmailMiddleware(req, res, next) {
140140
const ResetPasswordTokenModel = await Services.ResetPasswordToken.findByAccountId(user.id);
141141
//generate email
142142
const token = Services.ResetPasswordToken.generateToken(ResetPasswordTokenModel.id, user.id);
143-
const mailData = Services.ResetPasswordToken.generateResetPasswordEmail(req.hostname, req.body.email, token);
143+
const mailData = Services.ResetPasswordToken.generateResetPasswordEmail(process.env.FRONTEND_ADDRESS, req.body.email, token);
144144
if (mailData !== undefined) {
145145
Services.Email.send(mailData, (err) => {
146146
if (err) {
@@ -176,7 +176,7 @@ async function sendConfirmAccountEmailMiddleware(req, res, next) {
176176
await Services.AccountConfirmation.create(Constants.General.HACKER, account.email, account.id);
177177
const accountConfirmationToken = await Services.AccountConfirmation.findByAccountId(account.id);
178178
const token = Services.AccountConfirmation.generateToken(accountConfirmationToken.id, account.id);
179-
const mailData = Services.AccountConfirmation.generateAccountConfirmationEmail(req.hostname, account.email, Constants.General.HACKER, token);
179+
const mailData = Services.AccountConfirmation.generateAccountConfirmationEmail(process.env.FRONTEND_ADDRESS, account.email, Constants.General.HACKER, token);
180180
if (mailData !== undefined) {
181181
Services.Email.send(mailData, (err) => {
182182
if (err) {
@@ -214,7 +214,7 @@ async function resendConfirmAccountEmail(req, res, next) {
214214
});
215215
}
216216
const token = Services.AccountConfirmation.generateToken(accountConfirmationToken.id, account.id);
217-
const mailData = Services.AccountConfirmation.generateAccountConfirmationEmail(req.hostname, account.email, accountConfirmationToken.accountType, token);
217+
const mailData = Services.AccountConfirmation.generateAccountConfirmationEmail(process.env.FRONTEND_ADDRESS, account.email, accountConfirmationToken.accountType, token);
218218
if (mailData !== undefined) {
219219
Services.Email.send(mailData, (err) => {
220220
if (err) {

services/accountConfirmation.service.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,20 @@ function generateConfirmTokenLink(httpOrHttps, domain, type, token) {
9595
/**
9696
* Generates the mailData for the account confirmation Email. This really only applies to
9797
* hackers as all other accounts are intrinsically confirmed via the email they recieve to invite them
98-
* @param {string} hostname The hostname that this service is running on
98+
* @param {string} address The hostname that this service is running on
9999
* @param {string} receiverEmail The receiver of the email
100100
* @param {string} type the user type
101101
* @param {string} token The account confirmation token
102102
*/
103-
function generateAccountConfirmationEmail(hostname, receiverEmail, type, token) {
104-
const httpOrHttps = (hostname === "localhost") ? "http" : "https";
105-
const address = (hostname === "localhost") ? `localhost:${process.env.PORT}` : hostname;
103+
function generateAccountConfirmationEmail(address, receiverEmail, type, token) {
104+
const httpOrHttps = (address.includes("localhost")) ? "http" : "https";
106105
const tokenLink = generateConfirmTokenLink(httpOrHttps, address, type, token);
107106
var emailSubject = "";
108107
if (token === undefined || tokenLink === undefined) {
109108
return undefined;
110109
}
111110
if (type === Constants.HACKER) {
112-
emailSubject = Constants.CONFIRM_ACC_EMAIL_SUBJECT
111+
emailSubject = Constants.CONFIRM_ACC_EMAIL_SUBJECT;
113112
}
114113
const handlebarPath = path.join(__dirname, `../assets/email/AccountConfirmation.hbs`);
115114

@@ -125,30 +124,26 @@ function generateAccountConfirmationEmail(hostname, receiverEmail, type, token)
125124
}
126125

127126
/*
128-
* Generates the mailData for the account invitation Email.
129-
* @param {string} hostname The hostname that this service is running on
130-
* @param {string} receiverEmail The receiver of the email
131-
* @param {string} type The user type
132-
* @param {string} token The account confirmation token
127+
* Generates the mailData for the account invitation Email.
128+
* @param {string} address The hostname that this service is running on
129+
* @param {string} receiverEmail The receiver of the email
130+
* @param {string} type The user type
131+
* @param {string} token The account confirmation token
133132
*/
134-
function generateAccountInvitationEmail(hostname, receiverEmail, type, token) {
135-
const httpOrHttps = (hostname === "localhost") ? "http" : "https";
136-
const address = (hostname === "localhost") ? `localhost:${process.env.PORT}` : hostname;
133+
function generateAccountInvitationEmail(address, receiverEmail, type, token) {
134+
const httpOrHttps = (address.includes("localhost")) ? "http" : "https";
137135
const tokenLink = generateCreateAccountTokenLink(httpOrHttps, address, type, token);
138136
var emailSubject = "";
139137
if (token === undefined || tokenLink === undefined) {
140138
return undefined;
141139
}
142140
if (type === Constants.HACKER) {
143141
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.HACKER];
144-
}
145-
else if(type === Constants.VOLUNTEER){
142+
} else if (type === Constants.VOLUNTEER) {
146143
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.VOLUNTEER];
147-
}
148-
else if(Constants.SPONSOR_TIERS.includes(type)){
144+
} else if (Constants.SPONSOR_TIERS.includes(type)) {
149145
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.SPONSOR];
150-
}
151-
else if(type === Constants.STAFF){
146+
} else if (type === Constants.STAFF) {
152147
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.STAFF];
153148
}
154149
const handlebarPath = path.join(__dirname, `../assets/email/AccountInvitation.hbs`);

services/resetPassword.service.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,12 @@ function generateTokenLink(httpOrHttps, domain, token) {
8787

8888
/**
8989
* Generates the mailData for the resetPassword Email.
90-
* @param {string} hostname The hostname that this service is running on
90+
* @param {string} address The web address that the front-end service is running on
9191
* @param {string} receiverEmail The receiver of the email
9292
* @param {string} token The resetPassword token
9393
*/
94-
function generateResetPasswordEmail(hostname, receiverEmail, token) {
95-
const httpOrHttps = (hostname === "localhost") ? "http" : "https";
96-
const address = (hostname === "localhost") ? `localhost:${process.env.PORT}` : hostname;
94+
function generateResetPasswordEmail(address, receiverEmail, token) {
95+
const httpOrHttps = (address.includes("localhost")) ? "http" : "https";
9796
const tokenLink = generateTokenLink(httpOrHttps, address, token);
9897
if (token === undefined || tokenLink === undefined) {
9998
return undefined;

tests/email.service.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ describe("Email Service", function () {
4747
it("It should compile a handlebars email", (done) => {
4848
const handlebarPath = path.join(__dirname, `../assets/email/test.hbs`);
4949
const rendered = EmailService.renderEmail(handlebarPath, {
50-
TEST: "TESTTEST"
50+
TEST: "TESTTEST",
51+
NOT_ESCAPED: "localhost:1337/reset?token=lala"
5152
});
52-
assert.equal("<div>This is used for testing email service. DO NOT REMOVE.TESTTEST</div>", rendered);
53+
assert.equal('<div>This is used for testing email service. DO NOT REMOVE.TESTTEST. <a href="localhost:1337/reset?token=lala">link</a></div>', rendered);
5354
done();
5455
});
5556
});

0 commit comments

Comments
 (0)