-
Notifications
You must be signed in to change notification settings - Fork 17
/
users.js
117 lines (105 loc) · 2.96 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
const express = require("express");
const { check } = require("express-validator");
const asyncHandler = require("express-async-handler");
const { handleValidationErrors } = require("../../utils/validation");
const { setTokenCookie, requireAuth } = require("../../utils/auth");
const { User } = require("../../db/models");
const {
singleMulterUpload,
singlePublicFileUpload,
multipleMulterUpload,
multiplePublicFileUpload,
} = require("../../awsS3");
const router = express.Router();
const validateSignup = [
check("email")
.isEmail()
.withMessage("Please provide a valid email.")
.custom((value, { req }) => {
return new Promise((resolve, reject) => {
User.findOne({ where: { email: req.body.email } })
.then((res) => {
console.log("res.....", res);
if (res) {
reject("Email already taken");
} else {
resolve();
}
})
.catch((err) => {
rej("Database error: ", err.message);
});
});
}),
check("username")
.isLength({ min: 4 })
.withMessage("Please provide a username with at least 4 characters.")
.custom((value, { req }) => {
return new Promise((resolve, reject) => {
User.findOne({ where: { username: req.body.username } })
.then((res) => {
if (res) {
reject("Username already taken");
} else {
resolve();
}
})
.catch((err) => {
rej("Database error: ", err.message);
});
});
}),
check("username").not().isEmail().withMessage("Username cannot be an email."),
check("password")
.isLength({ min: 6 })
.withMessage("Password must be 6 characters or more."),
handleValidationErrors,
];
// Sign up
router.post(
"/",
singleMulterUpload("image"),
validateSignup,
asyncHandler(async (req, res) => {
const { email, password, username } = req.body;
const profileImageUrl = await singlePublicFileUpload(req.file);
const user = await User.signup({
username,
email,
password,
profileImageUrl,
});
setTokenCookie(res, user);
return res.json({
user,
});
})
);
// for multiple files
// router.post(
// "/",
// multipleMulterUpload("images"),
// asyncHandler(async (req, res) => {
// const images = await multiplePublicFileUpload(req.files);
// // Do something with the images
// // images is an array where each element is the url to your bucket.
// })
// );
router.put(
"/:id",
singleMulterUpload("image"),
asyncHandler(async (req, res) => {
const id = req.params.id;
const profileImageUrl = await singlePublicFileUpload(req.file);
await User.update({ profileImageUrl }, { where: { id } });
res.json({ profileImageUrl });
})
);
router.get(
"/",
asyncHandler(async (req, res) => {
const users = await User.findAll();
res.json(users);
})
);
module.exports = router;