translated error

This commit is contained in:
leca 2024-10-31 08:29:56 +03:00
parent c950882e0e
commit d4233c72f5
2 changed files with 3 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import AbstractProductService from '../services/abstractproduct.js';
import ProductService from '../services/product.js'; import ProductService from '../services/product.js';
import translate from '../utils/translate.js'; import translate from '../utils/translate.js';
import responseCodes from '../response/responseCodes.js'; import responseCodes from '../response/responseCodes.js';
import customError from '../response/customError.js';
const TAG = "/controllers/userjs" const TAG = "/controllers/userjs"
@ -23,7 +24,7 @@ class UserController {
const { username, password } = req.body; const { username, password } = req.body;
const user = await UserService.getByUsername(username); 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); const token = genToken(user);
return res.status(200).send(token); return res.status(200).send(token);

View File

@ -6,6 +6,7 @@ import errorHandler from '../utils/pgerrorhandler.js';
class UserService { class UserService {
async create(username, password) { 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) => { await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, bcrypt.hashSync(password, 12)]).catch((e) => {
errorHandler(e, "user"); errorHandler(e, "user");
}) })