-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
334 lines (315 loc) · 8.94 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use strict';
const crypto = require('crypto');
const ah = require('express-async-handler');
const router = require('express').Router();
// eslint-disable-next-line no-unused-vars
const logger = require('../../lib/logger')('USERS');
const models = require('../models');
const authenticate = require('./concerns/authenticate');
const MessageVerifier = require('../../lib/MessageVerifier');
const User = models.user;
const encodeToken = (token) => MessageVerifier.generate(token);
const getToken = async () => crypto
.randomBytes(16)
.toString('base64');
const userFilter = { passwordDigest: 0, token: 0 };
/* eslint-disable max-len */
/**
* @openapi
* tags:
* - name: User
* description: Interacts with the User schema
* components:
* schemas:
* ApiResponse:
* type: object
* properties:
* code:
* type: integer
* format: int32
* type:
* type: string
* message:
* type: string
* Credentials:
* type: object
* required:
* - email
* - password
* properties:
* email:
* type: string
* format: email
* password:
* type: string
* example: ThisisaReallyBadPassword
* password_confirmation:
* description: password confirmation field. Only checked on signup, should match sibling password
* type: string
* example: ThisisaReallyBadPassword
* User:
* type: object
* description: |
* Fields allowed to be edited:
* - email
* - password
* required:
* - id
* - email
* properties:
* _id:
* type: string
* email:
* type: string
* createdAt:
* type: string
* format: timestamp
* updatedAt:
* type: string
* format: timestamp
* token:
* type: string
* description: Bearer
*/
/* eslint-enable max-len */
/**
* @openapi
* /users:
* get:
* tags:
* - User
* summary: Get all users
* operationId: getAllUser
* security:
* - BearerAuth: []
* responses:
* "200":
* description: successful operation
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: "#/components/schemas/User"
* "404":
* description: User not found
*/
router.get('/', authenticate, ah(async (req, res) => {
const users = await User.find({}, userFilter);
return res.json(users);
}));
/**
* @openapi
* /users/{id}:
* get:
* tags:
* - User
* summary: Get user by user name
* operationId: getUserByName
* security:
* - BearerAuth: []
* parameters:
* - name: id
* in: path
* description: "The id of the user that needs to be fetched."
* required: true
* schema:
* type: string
* responses:
* "200":
* description: successful operation
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/User"
* "404":
* description: User not found
*/
router.get('/:id', authenticate, ah(async (req, res) => {
const user = await User.findById(req.params.id, userFilter);
if (!user) {
return res.status(404).json({ error: 'No such user' });
}
return res.json(user);
}));
/**
* @openapi
* /users/signup:
* post:
* tags:
* - User
* summary: Signs user up for the system
* description: ""
* operationId: signupUser
* requestBody:
* $ref: '#/components/schemas/Credentials'
* responses:
* "200":
* description: successful operation
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* "401":
* description: Invalid username/password supplied
*/
router.post('/signup', ah(async (req, res) => {
const credentials = req.body.credentials || req.body;
if (!credentials || !credentials.email || !credentials.password) {
return res.status(400).json({ error: 'Bad Request. No `credentials`.' });
}
const userCriteria = { email: credentials.email, password: credentials.password };
userCriteria.token = await getToken();
const presave = new User(userCriteria);
const created = await presave.save();
const user = created.toObject();
delete user.token;
delete user.passwordDigest;
delete user.__v;
return res.json(user);
}));
/**
* @openapi
* /users/login:
* post:
* tags:
* - User
* summary: Logs user into the system
* description: ""
* operationId: loginUser
* security:
* - Basic: []
* responses:
* "200":
* description: successful operation
* headers:
* X-Rate-Limit:
* description: calls per hour allowed by the user
* schema:
* type: integer
* format: int32
* X-Expires-After:
* description: date in UTC when token expires
* schema:
* type: string
* format: date-time
* content:
* application/json:
* schema:
* type: string
* "401":
* description: Invalid username/password supplied
*/
router.post('/login', ah(async (req, res) => {
let credentials;
if (req.headers.authorization && req.headers.authorization.startsWith('Basic')) {
const encoded = req.headers.authorization.split(' ')[1];
if (!encoded) {
return res.status(401).json({ error: 'Invalid authorization' });
}
const plain = Buffer.from(encoded, 'base64').toString().split(':').filter((s) => s.length);
if (plain.length !== 2) {
return res.status(401).json({ error: 'Invalid authorization' });
}
credentials = {
email: plain[0],
password: plain[1],
};
} else {
return res.status(401).json({ error: 'Invalid authorization' });
}
const search = { email: credentials.email };
let user = await User.findOne(search);
const token = user.comparePassword(credentials.password);
user.token = token;
user = user.toObject();
delete user.passwordDigest;
user.token = encodeToken(user.token);
delete user.__v;
return res.status(200).json(user);
}));
/**
* @openapi
* /users/logout:
* delete:
* tags:
* - User
* summary: Logs out current logged in user session
* description: ""
* operationId: logoutUser
* security:
* - BearerAuth: []
* responses:
* default:
* description: successful operation
*/
router.delete('/logout', authenticate, ah(async (req, res) => {
const token = await getToken();
await User.findOneAndUpdate({
_id: req.currentUser._id,
token: req.currentUser.token,
}, {
token,
});
return res.status(200).end();
}));
/**
* @openapi
* "/users/{id}":
* patch:
* tags:
* - User
* summary: Updated user
* description: This can only be done by the logged in user.
* operationId: updateUser
* security:
* - BearerAuth: []
* parameters:
* - name: id
* in: path
* description: name that need to be updated
* required: true
* schema:
* type: string
* requestBody:
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/User"
* description: Updated user object
* required: true
* responses:
* "400":
* description: Invalid user supplied
* "404":
* description: User not found
* "200":
* description: User updated
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/User"
*/
router.patch('/:id', authenticate, ah(async (req, res) => {
const user = await User.findOne({
_id: req.params.id,
token: req.currentUser.token,
});
if (!user) return res.status(404).json({ error: 'No such user' });
const hasEdit = !!(req?.body?.password || req?.body?.email);
if (!hasEdit) return res.status(400).json({ error: 'No modified field.' });
const query = { $set: {} };
// found this solution here: https://stackoverflow.com/a/54734798/2518037
Object.keys(req.body)
.filter((key) => !['_id', 'password'].includes(key))
.forEach((key) => {
query.$set[key] = req.body[key];
});
const updatedUser = await User.findOneAndUpdate({ _id: req.params.id }, query).exec();
if (req.body.password) {
updatedUser.password = req.body.password;
await updatedUser.save();
}
return res.status(200).json(updatedUser.toObject()).end();
}));
module.exports = router;