-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
210 lines (200 loc) · 6.04 KB
/
users.js
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
const router = require('express').Router(),
passport = require('./middleware/passport'),
tokenizer = require('../../auth/tokenizer'),
Controller = require('../../controller/users'),
{ User, ExUser, Applicant } = require('../../models');
// All routes prefixed with '/api/users'
// refresh token
router.route('/refresh').get(tokenizer.refresh());
router // get a user document
.route('/get/:userId')
.get(tokenizer.guard(), (req, res) => Controller.find(req, res, User));
router // get a user's front-end data
.route('/data/:userId')
.get(tokenizer.guard(), async (req, res) => {
res.json(await Controller.contact(req.params.userId, User));
});
router // confirm user's permission to start donating (eligibility)
.route('/privilege/:userId')
.get(tokenizer.guard(), async (req, res) =>
res.json(await Controller.certify(req.params.userId, User))
);
router // give user permission to start donating (eligibility)
.route('/privilege/:userId')
.patch(tokenizer.guard(), (req, res) =>
res.json(Controller.empower(req.params.userId, User))
);
router // check user permission to donate full amount (has filled out profile)
.route('/promote/:userId')
.get(tokenizer.guard(), async (req, res) =>
res.json(await Controller.deem(req.params.userId, User))
);
router // give user permission to donate full amount (has filled out profile)
.route('/promote/:userId')
.patch(tokenizer.guard(), (req, res) =>
res.json(Controller.promote(req.params.userId, User))
);
router // update account
.route('/update/:userId')
.put(tokenizer.guard(), (req, res) => Controller.update(req, res, User));
// create new user account
router.route('/').post(async (req, res) => {
const fail = {
isSignupHashConfirmed: false,
isLinkExpired: false,
};
let anyUser = await Controller.verify(
req.body.username,
'username',
User
);
// if (!!anyUser)
// anysuer = await Controller.verify(
// req.body.username,
// 'username',
// Applicant
// );
if (!!anyUser)
anysuer = await Controller.verify(
req.body.username,
'username',
ExUser
);
if (!!anyUser) {
res.json(fail); // send warning email to existing User?
} else {
const newAccount = await Controller.create(
req.body,
Applicant,
ExUser
);
if (newAccount === 'undefined') {
res.json(fail);
} else {
res.json(newAccount);
}
}
});
router
.route('/activate/:hash')
.get(async (req, res) =>
res.json(
await Controller.activate(req.params.hash, Applicant, User, ExUser)
)
);
router // give user permission to donate full amount (if filled out account profile)
.route('/promote/:userId')
.patch(tokenizer.guard(), (req, res) =>
res.json(Controller.promote(req.params.userId, User))
);
// change password
router.route('/change/:userId').put(tokenizer.guard(), (req, res) => {
res.json(Controller.change(req, res, User));
});
// reset password
router.route('/forgot').put(async (req, res) => {
res.json(await Controller.forgot(req.body.email, User));
// what about accounts not yet activated?
});
router.route('/reset/:hash').get(async (req, res) => {
if (await Controller.rattle('resetPasswordHash', req.params.hash, User))
res.json({
isHashConfirmed: false,
isLinkExpired: false,
});
else res.json(await Controller.confirm(req.params.hash, User));
});
router.route('/reset').put(async (req, res) => {
const args = {
...req.body,
model: User,
emailTemplate: 'Reset',
};
const validation = await Controller.validate(args);
if (typeof validation === 'number') {
if (validation < 3) {
res.json(validation); // define?
} else {
req.logout();
res.json('This account has been locked.');
}
} else if (validation === true) {
const reset = await Controller.reset(req, res, User);
if (reset) {
req.logout();
} else {
// return err
}
}
});
// delete account
router
.route('/delete/:userId')
.delete(tokenizer.guard(), async (req, res) => {
res.json(await Controller.remove(req.params.userId, User, ExUser));
});
// login and out
router
.route('/login')
.post(passport.authenticate('local-login'), async (req, res) => {
const userDoc = await req.user._doc;
if (await Controller.rattle('username', req.body.username, User))
res.sendStatus(401).json();
else
res.json({
id: userDoc._id,
zip: userDoc.zip ?? '',
city: userDoc.city ?? '',
email: userDoc.email ?? '', // ?? userDoc.username,
state: userDoc.state ?? '',
username: userDoc.username,
accessToken: req.user.accessToken,
payment: userDoc.payment ?? {
payment_method: '',
customer_id: '',
},
refreshToken: req.user.refreshToken,
address: userDoc.address ?? '',
country: userDoc.country ?? '',
credits: userDoc.credits ?? {},
isEmployed: userDoc.isEmployed,
settings: userDoc.settings ?? {
emailReceipts: true,
showToolTips: true,
autoTweet: false,
},
employer: userDoc.employer ?? '',
lastName: userDoc.lastName ?? '',
passport: userDoc.passport ?? '',
firstName: userDoc.firstName ?? '',
occupation: userDoc.occupation ?? '',
phoneNumber: userDoc.phoneNumber ?? '',
isCompliant: userDoc.isCompliant ?? false,
understands: userDoc.understands ?? false,
});
});
router.route('/logout').get(tokenizer.guard(), (req, res, next) => {
req.logout((err) => {
if (err) {
return next(err);
}
});
res.json(true);
});
// route for retrieving info of current signed in user and for isAuth component
router.route('/currentuser').get(tokenizer.guard(), (req, res) => {
if (!req.user) {
// The user is not logged in, send back an empty object
res.json({
username: '',
id: '',
});
} else {
// Otherwise send back the user's email and id
res.json({
username: req.user.username,
id: req.user._id,
});
}
});
module.exports = router;