diff --git a/src/controllers/user.js b/src/controllers/user.js index 7d38dfa..49a4864 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -6,6 +6,7 @@ import AbstractProductService from '../services/abstractproduct.js'; import ProductService from '../services/product.js'; import translate from '../utils/translate.js'; import responseCodes from '../response/responseCodes.js'; +import customError from '../response/customError.js'; const TAG = "/controllers/userjs" @@ -23,7 +24,7 @@ class UserController { const { username, password } = req.body; const user = await UserService.getByUsername(username); - if (!bcrypt.compareSync(password, user.password)) return res.status(401).send("Wrong password"); + if (!bcrypt.compareSync(password, user.password)) throw new customError(`Wrong user password`, responseCodes.responses.passwords.invalid); const token = genToken(user); return res.status(200).send(token); diff --git a/src/services/user.js b/src/services/user.js index 5b16367..10997b5 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -6,6 +6,7 @@ import errorHandler from '../utils/pgerrorhandler.js'; class UserService { async create(username, password) { + if (await this.getByUsername(username)) throw new customError(`Such user exists`, responseCodes.responses.user.duplicate); await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, bcrypt.hashSync(password, 12)]).catch((e) => { errorHandler(e, "user"); })