-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaccountConfirmation.service.js
More file actions
203 lines (194 loc) Β· 6.93 KB
/
accountConfirmation.service.js
File metadata and controls
203 lines (194 loc) Β· 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";
const logger = require("./logger.service");
const AccountConfirmation = require("../models/accountConfirmationToken.model");
const Constants = require("../constants/general.constant");
const jwt = require("jsonwebtoken");
const path = require("path");
const Services = {
Email: require("./email.service")
};
/**
* @function findByAccountId
* @param {ObjectId} accountId
* @return {DocumentQuery} The document query will resolve to either account confirmation or null.
* @description Finds an account confirmation document by accountId.
*/
function findByAccountId(accountId) {
const TAG = `[ AccountConfirmation Reset Service # findByAccountId ]`;
return AccountConfirmation.findOne(
{
accountId: accountId
},
logger.queryCallbackFactory(TAG, "AccountConfirmation", "accountId")
);
}
/**
* @function findById
* @param {ObjectId} id
* @return {DocumentQuery} The document query will resolve to either account confirmation or null.
* @description Finds an account by mongoID.
*/
function findById(id) {
const TAG = `[ AccountConfirmation Service # findById ]`;
return AccountConfirmation.findById(
id,
logger.queryCallbackFactory(TAG, "AccountConfirmation", "mongoId")
);
}
/**
* @function find
* @param {*} query the query to search the database by.
* @return {DocumentQuery<any[]>} The document query will resolve to either account confirmations or null.
* @description Finds an account by query.
*/
function find(query) {
const TAG = `[ AccountConfirmation Service # find ]`;
return AccountConfirmation.find(
query,
logger.queryCallbackFactory(TAG, "AccountConfirmation", query)
);
}
/**
* Creates Account Confirmation document in the database
* @param {String} type the type of user which to create the token for
* @param {String} email
* @param {ObjectId} accountId optional accountId parameter to link to account, optional when token is being made for not a hacker
* @returns {Promise.<*>}
*/
async function create(type, email, accountId) {
//Create new instance of account confirmation
const newAccountToken = AccountConfirmation({
accountType: type,
email: email
});
if (accountId !== undefined) {
newAccountToken.accountId = accountId;
}
return newAccountToken.save();
}
/**
* Generates JWT for Confirming account
* @param {ObjectId} accountConfirmationId
* @param {ObjectId} accountId
* @returns {string} JWT Token containing accountId and accountConfirmationId
*/
function generateToken(accountConfirmationId, accountId = null) {
const token = jwt.sign(
{
accountConfirmationId: accountConfirmationId,
accountId: accountId
},
process.env.JWT_CONFIRM_ACC_SECRET,
{
expiresIn: "7 day"
}
);
return token;
}
/**
* Generates the link that the user will use to access the page to begin account creationg
* @param {'http'|'https'} httpOrHttps
* @param {string} domain the domain of the current
* @param {string} type the model that the
* @param {string} token the reset token
* @returns {string} the string, of form: [http|https]://{domain}/{model}/create?token={token}
*/
function generateCreateAccountTokenLink(httpOrHttps, domain, type, token) {
const link = `${httpOrHttps}://${domain}/account/create?token=${token}&accountType=${type}`;
return link;
}
/**
* Generates the link that the user will use to confirm account and proceed with account creationg
* @param {'http'|'https'} httpOrHttps
* @param {string} domain the domain of the current
* @param {string} type the model that the
* @param {string} token the reset token
* @returns {string} the string, of form: [http|https]://{domain}/{model}/create?token={token}
*/
function generateConfirmTokenLink(httpOrHttps, domain, token) {
const link = `${httpOrHttps}://${domain}/account/confirm?token=${token}`;
return link;
}
/**
* Generates the mailData for the account confirmation Email. This really only applies to
* hackers as all other accounts are intrinsically confirmed via the email they recieve to invite them
* @param {string} address The hostname that this service is running on
* @param {string} receiverEmail The receiver of the email
* @param {string} type the user type
* @param {string} token The account confirmation token
*/
function generateAccountConfirmationEmail(address, receiverEmail, type, token) {
const httpOrHttps = address.includes("localhost") ? "http" : "https";
const tokenLink = generateConfirmTokenLink(httpOrHttps, address, token);
var emailSubject = "";
if (token === undefined || tokenLink === undefined) {
return undefined;
}
if (type === Constants.HACKER) {
emailSubject = Constants.CONFIRM_ACC_EMAIL_SUBJECT;
}
const handlebarPath = path.join(
__dirname,
`../assets/email/AccountConfirmation.hbs`
);
const mailData = {
from: process.env.NO_REPLY_EMAIL,
to: receiverEmail,
subject: emailSubject,
html: Services.Email.renderEmail(handlebarPath, {
link: tokenLink
})
};
return mailData;
}
/*
* Generates the mailData for the account invitation Email.
* @param {string} address The hostname that this service is running on
* @param {string} receiverEmail The receiver of the email
* @param {string} type The user type
* @param {string} token The account confirmation token
*/
function generateAccountInvitationEmail(address, receiverEmail, type, token) {
const httpOrHttps = address.includes("localhost") ? "http" : "https";
const tokenLink = generateCreateAccountTokenLink(
httpOrHttps,
address,
type,
token
);
var emailSubject = "";
if (token === undefined || tokenLink === undefined) {
return undefined;
}
if (type === Constants.HACKER) {
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.HACKER];
} else if (type === Constants.VOLUNTEER) {
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.VOLUNTEER];
} else if (Constants.SPONSOR_TIERS.includes(type)) {
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.SPONSOR];
} else if (type === Constants.ADMIN) {
emailSubject = Constants.CREATE_ACC_EMAIL_SUBJECTS[Constants.ADMIN];
}
const handlebarPath = path.join(
__dirname,
`../assets/email/AccountInvitation.hbs`
);
const mailData = {
from: process.env.NO_REPLY_EMAIL,
to: receiverEmail,
subject: emailSubject,
html: Services.Email.renderEmail(handlebarPath, {
link: tokenLink
})
};
return mailData;
}
module.exports = {
find: find,
findById: findById,
findByAccountId: findByAccountId,
create: create,
generateToken: generateToken,
generateAccountConfirmationEmail: generateAccountConfirmationEmail,
generateAccountInvitationEmail: generateAccountInvitationEmail
};