Skip to content

Commit c344ea0

Browse files
committed
fix some errors
1 parent d79285c commit c344ea0

File tree

7 files changed

+27
-72
lines changed

7 files changed

+27
-72
lines changed

app.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ app.get("/crash-test", () => {
2828
}, 0);
2929
});
3030

31+
app.use(requestLogger);
3132
app.post("/signup", validateUserBody, createUser);
3233
app.post("/signin", validateUserLogIn, login);
3334
app.get("/items", getClothingItem);
3435

3536
app.use(auth);
3637

37-
app.use(requestLogger);
38-
3938
app.use("/", mainRouter);
4039

4140
app.use(errorLogger);

controllers/users.js

+1-50
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
const bcrypt = require("bcryptjs");
22
const jwt = require("jsonwebtoken");
33
const User = require("../models/user");
4-
// const {
5-
// invalidDataError,
6-
// authenticationError,
7-
// notFoundError,
8-
// defaultError,
9-
// conflictError,
10-
// } = require("../utils/errors");
114
const { JWT_SECRET } = require("../utils/config");
125
const BadRequestError = require("../errors/bad-request-err");
136
const UnauthorizedError = require("../errors/unauthorized-err");
@@ -22,22 +15,13 @@ const getCurrentUser = (req, res, next) => {
2215
console.log(err.name);
2316
if (err.name === "CastError") {
2417
next(new BadRequestError("Bad Request! Invalid data passed"));
25-
// return res
26-
// .status(invalidDataError)
27-
// .send({ message: "Bad Request! Invalid data passed" });
2818
} else if (err.name === "DocumentNotFoundError") {
2919
next(
3020
new NotFoundError(" The request was sent to a non-existent address")
3121
);
32-
// return res
33-
// .status(notFoundError)
34-
// .send({ message: " The request was sent to a non-existent address" });
3522
} else {
3623
next(err);
3724
}
38-
// return res
39-
// .status(defaultError)
40-
// .send({ message: "An error has occurred on the server" });
4125
});
4226
};
4327

@@ -46,9 +30,6 @@ const createUser = (req, res, next) => {
4630

4731
if (!email) {
4832
next(new BadRequestError("Email or Password is required!"));
49-
// return res
50-
// .status(invalidDataError)
51-
// .send({ message: "Email or Password is required!" });
5233
}
5334

5435
return User.findOne({ email })
@@ -72,20 +53,11 @@ const createUser = (req, res, next) => {
7253
console.error(err);
7354
if (err.code === 11000) {
7455
next(new ConflictError("The email already exists!"));
75-
// return res
76-
// .status(conflictError)
77-
// .send({ message: "The email already exists!" });
7856
} else if (err.name === "ValidationError") {
7957
next(new BadRequestError("Bad Request! Invalid data passed"));
80-
// return res
81-
// .status(invalidDataError)
82-
// .send({ message: "Bad Request! Invalid data passed" });
8358
} else {
8459
next(err);
8560
}
86-
// return res
87-
// .status(defaultError)
88-
// .send({ message: "An error has occurred on the server" });
8961
});
9062
};
9163

@@ -102,27 +74,15 @@ const updateUser = (req, res, next) => {
10274
console.log(err.name);
10375
if (err.name === "ValidationError") {
10476
next(new BadRequestError("Bad Request! Invalid data passed"));
105-
// return res
106-
// .status(invalidDataError)
107-
// .send({ message: "Bad Request! Invalid data passed" });
10877
} else if (err.name === "CastError") {
10978
next(new BadRequestError("Bad Request! Invalid data passed"));
110-
// return res
111-
// .status(invalidDataError)
112-
// .send({ message: "Bad Request! Invalid data passed" });
11379
} else if (err.name === "DocumentNotFoundError") {
11480
next(
11581
new NotFoundError(" The request was sent to a non-existent address")
11682
);
117-
// return res
118-
// .status(notFoundError)
119-
// .send({ message: " The request was sent to a non-existent address" });
12083
} else {
12184
next(err);
12285
}
123-
// return res
124-
// .status(defaultError)
125-
// .send({ message: "An error has occurred on the server" });
12686
});
12787
};
12888

@@ -131,31 +91,22 @@ const login = (req, res, next) => {
13191

13292
if (!email || !password) {
13393
next(new BadRequestError("Email and password are required"));
134-
// return res
135-
// .status(invalidDataError)
136-
// .send({ message: "Email and password are required" });
13794
}
13895

13996
return User.findUserByCredentials(email, password)
14097
.then((user) => {
14198
const token = jwt.sign({ _id: user._id }, JWT_SECRET, {
14299
expiresIn: "7d",
143100
});
144-
res.send({ user, token });
101+
res.send({ token });
145102
})
146103
.catch((err) => {
147104
console.error(err);
148105
if (err.message === "Incorrect email or password") {
149106
next(new UnauthorizedError("Incorrect email or password!"));
150-
// return res
151-
// .status(authenticationError)
152-
// .send({ message: "Incorrect email or password!" });
153107
} else {
154108
next(err);
155109
}
156-
// return res
157-
// .status(defaultError)
158-
// .send({ message: "An error has occurred on the server" });
159110
});
160111
};
161112

middlewares/auth.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
const jwt = require("jsonwebtoken");
22
const { JWT_SECRET } = require("../utils/config");
3-
const { authenticationError } = require("../utils/errors");
3+
const UnauthorizedError = require("../errors/unauthorized-err");
44

55
module.exports = (req, res, next) => {
66
const { authorization } = req.headers;
77

88
if (!authorization || !authorization.startsWith("Bearer ")) {
9-
return res
10-
.status(authenticationError)
11-
.send({ message: "Authorization Required" });
9+
next(new UnauthorizedError("Authorization Required"));
1210
}
1311

1412
const token = authorization.replace("Bearer ", "");
@@ -18,9 +16,7 @@ module.exports = (req, res, next) => {
1816
try {
1917
playload = jwt.verify(token, JWT_SECRET);
2018
} catch (err) {
21-
return res
22-
.status(authenticationError)
23-
.send({ message: "Authorization Required" });
19+
next(new UnauthorizedError("Authorization Required"));
2420
}
2521

2622
req.user = playload;

middlewares/validation.js

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ module.exports.validateUserLogIn = celebrate({
5757
}),
5858
});
5959

60+
module.exports.validateUserUpdate = celebrate({
61+
body: Joi.object().keys({
62+
name: Joi.string().required().min(2).max(30).messages({
63+
"string.min": 'The minimum length of the "name" field is 2',
64+
"string.max": 'The maximum length of the "name" field is 30',
65+
"string.empty": 'The "name" field must be filled in',
66+
}),
67+
avatar: Joi.string().required().custom(validateURL).messages({
68+
"string.empty": 'The "avatar" filed must be filled in',
69+
"string.uri": 'The "avatar" must be a valid url',
70+
}),
71+
}),
72+
});
73+
6074
module.exports.validateId = celebrate({
6175
params: Joi.object().keys({
6276
itemId: Joi.string().alphanum().length(24),

routes/clothingItems.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { validateCardBody, validateId } = require("../middlewares/validation");
99

1010
router.post("/", validateCardBody, createClothingItem);
1111
router.delete("/:itemId", validateId, deleteClothingItem);
12-
router.put("/:itemId/likes", likeItem);
13-
router.delete("/:itemId/likes", dislikeItem);
12+
router.put("/:itemId/likes", validateId, likeItem);
13+
router.delete("/:itemId/likes", validateId, dislikeItem);
1414

1515
module.exports = router;

routes/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const router = require("express").Router();
22
const userRouter = require("./users");
33
const clothingItemsRouter = require("./clothingItems");
4-
const { notFoundError } = require("../utils/errors");
4+
const NotFoundError = require("../errors/not-found-err");
55

66
router.use("/users", userRouter);
77
router.use("/items", clothingItemsRouter);
88

9-
router.use((req, res) => {
10-
res.status(notFoundError).send({
11-
message: "Requested resource not found",
12-
});
9+
router.use((req, res, next) => {
10+
next(new NotFoundError("Requested resource not found"));
1311
});
1412

1513
module.exports = router;

routes/users.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
const router = require("express").Router();
2-
const {
3-
getCurrentUser,
4-
5-
updateUser,
6-
} = require("../controllers/users");
2+
const { getCurrentUser, updateUser } = require("../controllers/users");
3+
const { validateUserUpdate } = require("../middlewares/validation");
74

85
router.get("/me", getCurrentUser);
9-
router.patch("/me", updateUser);
6+
router.patch("/me", validateUserUpdate, updateUser);
107

118
module.exports = router;

0 commit comments

Comments
 (0)