forked from confirmedcode/Main
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-controller.js
More file actions
301 lines (277 loc) · 7.64 KB
/
Copy pathuser-controller.js
File metadata and controls
301 lines (277 loc) · 7.64 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const ConfirmedError = require("shared/error");
const Logger = require("shared/logger");
// Middleware
const authenticate = require("../middleware/authenticate.js");
const { check, body, query, oneOf } = require("express-validator/check");
const validateCheck = require("../middleware/validate-check.js");
const passwordRules = require("../middleware/password-rules.js");
const { BruteForce } = require("shared/redis");
// Models
const { User } = require("shared/models");
const { Receipt } = require("shared/models");
// Constants
const VALID_PLATFORMS = ["android", "ios", "mac", "windows"];
// Routes
const router = require("express").Router();
/*********************************************
*
* Sign Up Page
*
*********************************************/
router.get("/signup",
[
query("refer"),
validateCheck
],
(request, response, next) => {
const refer = request.values.refer;
var chain = Promise.resolve();
if (refer) {
chain = User.getReferrerUserId(refer);
}
return chain
.then(referrerUserId => {
if (referrerUserId) {
response.render("signup", {
refer: refer
});
}
else {
response.render("signup");
}
})
.catch( error => { next(error); });
});
router.get("/signup-success",
(request, response, next) => {
response.render("signup-success");
});
/*********************************************
*
* Create User with Email
*
*********************************************/
router.post("/signup",
[
BruteForce(50),
body("email")
.exists().withMessage("Missing email address.")
.isEmail().withMessage("Invalid email address.")
.normalizeEmail(),
body("password")
.exists().withMessage("Missing password.")
.not().isEmpty().withMessage("Missing password.")
.custom(value => {
passwordRules(value);
return value;
}),
check("browser")
.toBoolean(false),
oneOf([
body("refer")
.isAlphanumeric().withMessage("Referral code must be alphanumeric."),
body("refer")
.isEmpty()
]),
validateCheck
],
(request, response, next) => {
const email = request.values.email;
const password = request.values.password;
const browser = request.values.browser;
const refer = request.values.refer;
var chain = Promise.resolve();
if (refer) {
chain = User.getReferrerUserId(refer);
}
return chain
.then(referrerUserId => {
return User.createWithEmailAndPassword(email, password, browser, referrerUserId);
})
.then(user => {
if (browser) {
response.redirect("/signup-success");
}
else {
response.status(200).json({
code: 1,
message: "Email Confirmation Sent"
});
}
})
.catch( error => next(error) );
});
/*********************************************
*
* Confirm Email
*
*********************************************/
router.get(["/confirm-email"],
[
BruteForce(50),
query("email")
.exists().withMessage("Missing email.")
.not().isEmpty().withMessage("Missing email.")
.isEmail().withMessage("Invalid email address."),
query("code")
.exists().withMessage("Missing confirmation code.")
.not().isEmpty().withMessage("Missing confirmation code.")
.isAlphanumeric().withMessage("Invalid confirmation code.")
.trim(),
query("browser")
.toBoolean(false),
validateCheck
],
(request, response, next) => {
const email = decodeURI(request.values.email);
const code = request.values.code;
const browser = request.values.browser;
return User.confirmEmail(code, email)
.then(success => {
if (browser) {
return request.flashRedirect("success", "Email confirmed. Please sign in.", "/signin?redirecturi=" + encodeURI("/new-subscription?browser=true"));
}
else {
return response.render("confirm-email-success");
}
})
.catch( error => next(error) );
});
/*********************************************
*
* Resend Confirmation Code
*
*********************************************/
router.get("/resend-confirm-code",
(request, response, next) => {
response.render("resend-confirm-code");
});
router.post("/resend-confirm-code",
[
BruteForce(20),
body("email")
.exists().withMessage("Missing email address.")
.isEmail().withMessage("Invalid email address.")
.normalizeEmail(),
validateCheck
],
(request, response, next) => {
const email = request.values.email;
User.resendConfirmCode(email)
.then( results => {
request.flashRedirect("info", "Confirmation email re-sent. Be sure to check your spam folder, as sometimes the email can get stuck there.", "/signin");
})
.catch( error => next(error) );
});
/*********************************************
*
* Convert Shadow User - Add Email/Password
*
*********************************************/
router.post("/convert-shadow-user",
[
BruteForce(30),
authenticate.checkAndSetUser,
body("newemail")
.exists().withMessage("Missing email address.")
.isEmail().withMessage("Invalid email address.")
.normalizeEmail(),
body("newpassword")
.exists().withMessage("Missing password.")
.not().isEmpty().withMessage("Missing password.")
.custom(value => {
passwordRules(value);
return value;
}),
validateCheck
],
(request, response, next) => {
const newEmail = request.values.newemail;
const newPassword = request.values.newpassword;
if (request.user.emailHashed && request.user.emailConfirmed == true) {
return next(new ConfirmedError(400, 48, "Can't convert shadow user that already has a confirmed email."));
}
return request.user.convertShadowUser(newEmail, newPassword)
.then( result => {
response.status(200).json({
code: 1,
message: "Email Confirmation Sent"
});
})
.catch( error => next(error) );
});
/*********************************************
*
* Client Download Page
*
*********************************************/
router.get("/clients",
(request, response, next) => {
response.render("clients");
});
/*********************************************
*
* Get Key
*
*********************************************/
router.post("/get-key",
[
BruteForce(200),
authenticate.checkAndSetUser,
check("platform")
.isIn(VALID_PLATFORMS).withMessage("Unrecognized platform."),
validateCheck
],
(request, response, next) => {
const platform = request.values.platform;
return request.user.getKey(platform)
.then(config => {
response.status(200).json(config);
})
.catch(error => { next(error); });
});
/*********************************************
*
* Do Not Email
*
*********************************************/
router.get("/do-not-email",
[
BruteForce(200),
check("email")
.exists().withMessage("Missing email address.")
.isEmail().withMessage("Invalid email address.")
.normalizeEmail(),
check("code")
.isAlphanumeric().withMessage("Code must be alphanumeric"),
validateCheck
],
(request, response, next) => {
const email = request.values.email;
const code = request.values.code;
response.render("do-not-email", {
code: code,
email: email
});
});
router.post("/do-not-email",
[
BruteForce(20),
body("email")
.exists().withMessage("Missing email address.")
.isEmail().withMessage("Invalid email address.")
.normalizeEmail(),
body("code")
.isAlphanumeric().withMessage("Code must be alphanumeric"),
validateCheck
],
(request, response, next) => {
const email = request.values.email;
const code = request.values.code;
User.setDoNotEmail(email, code)
.then( result => {
request.flashRedirect("info", "You will no longer receive any emails from us.", "/signin");
})
.catch(error => { next(error); });
});
module.exports = router;