-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.js
49 lines (42 loc) · 1.28 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
import express from "express";
import path from 'path';
import User, { createUser } from "../models/user.js";
import { generateToken, EXPIRATION_TIME } from "../auth.js";
const router = express.Router();
router.post("/", async (req, res) => { // Criar novo usuario
if (req.body) {
await createUser(req.body)
.then((user) => {
const token = generateToken(user.id);
res.cookie("SESSIONID", token, {
httpOnly: true,
signed: true,
maxAge: EXPIRATION_TIME * 1000,
});
res.send({ user, token });
// res.sendFile(path.join(req.context.front, "area-do-consumidor.html"));
})
.catch((err) => {
if (err.name === "MongoError" && err.code === 11000) {
res.status(422).json({ error: "Email must be unique" });
} else {
res.status(422).json({ error: err.message });
}
});
} else {
res.statusCode(400);
}
});
router.get("/", (req, res) => { // Resgatar um usuario a partir do userId
if (!req.body.userId) {
res.status(422).json({ error: "Inform the userId key" });
}
User.findById(req.body.userId, (err, doc) => {
if (err) {
res.status(422).json({ error: err.message });
} else {
res.json(doc);
}
});
});
export default router;