-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsersController.js
40 lines (33 loc) · 1.12 KB
/
UsersController.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
/* eslint-disable import/no-named-as-default */
import sha1 from 'sha1';
import Queue from 'bull/lib/queue';
import dbClient from '../utils/db';
const userQueue = new Queue('email sending');
export default class UsersController {
static async postNew(req, res) {
const email = req.body ? req.body.email : null;
const password = req.body ? req.body.password : null;
if (!email) {
res.status(400).json({ error: 'Missing email' });
return;
}
if (!password) {
res.status(400).json({ error: 'Missing password' });
return;
}
const user = await (await dbClient.usersCollection()).findOne({ email });
if (user) {
res.status(400).json({ error: 'Already exist' });
return;
}
const insertionInfo = await (await dbClient.usersCollection())
.insertOne({ email, password: sha1(password) });
const userId = insertionInfo.insertedId.toString();
userQueue.add({ userId });
res.status(201).json({ email, id: userId });
}
static async getMe(req, res) {
const { user } = req;
res.status(200).json({ email: user.email, id: user._id.toString() });
}
}