-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (59 loc) · 2.14 KB
/
index.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
const UserModel = require("./models/user");
const server = require("./setup/server");
const jwt = require('./setup/jwt');
const md5 = require("md5");
const authMiddleware = async (req, res, next) => {
const [, token] = req.headers.authorization.split(' ');
try {
const payload = jwt.verify(token);
const user = await UserModel.findById(payload.user);
if (!user) res.send(401);
req.auth = user;
next();
} catch(error) {
res.sendStatus(401);
}
}
server.post('/cadastro', async (req, res) => {
try {
const { nameBody, passBody, emailBody } = req.body;
const user = await UserModel.novoUsuario(nameBody, passBody, emailBody);
const token = jwt.sign({ user: emailBody });
res.status(201).send({ user, token});
} catch (error) {
res.sendStatus(401);
}
});
server.get('/login', async (req, res) => {
try {
const [, hash] = req.headers.authorization.split(' ');
const [emailBody, passBody] = Buffer.from(hash, 'base64').toString().split(':');
const credenciais = await UserModel.findOne(emailBody, passBody);
if (credenciais.length === 0) return res.sendStatus(401);
else {
//variáveis para tratar retornos do banco (email, pass)
const emailDB = await credenciais[0].EMAIL;
const passDB = await credenciais[0].PASS;
if (emailDB === emailBody && passDB === md5(passBody)) {
//retornar confirmação das credenciais + token
const token = jwt.sign({ user: emailBody });
return res.status(200).send({ credenciais, token });
} else {
return res.sendStatus(401);
}
}
} catch(error) {
res.sendStatus(401);
}
});
server.get('/users', authMiddleware, async (req, res) => {
try {
const users = await UserModel.find();
res.send(users);
} catch (error) {
res.sendStatus(401);
}
});
server.get('/me', authMiddleware, async (req, res) => {
res.send(req.auth);
});