From 69c2255614b51e825f7456a73fadeb34696ce015 Mon Sep 17 00:00:00 2001 From: leca Date: Sun, 3 Nov 2024 02:41:13 +0300 Subject: [PATCH] now sending userid on login and register --- src/controllers/user.js | 6 +++--- src/services/user.js | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/controllers/user.js b/src/controllers/user.js index df73119..ef166aa 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -15,10 +15,10 @@ class UserController { async register(req, res) { const { username, password } = req.body; - await UserService.create(username, password); + let userId = await UserService.create(username, password); log.info(`New user with name ${username} has just registered`); - return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); + return res.status(200).send(userId.toString()); } async login(req, res) { @@ -28,7 +28,7 @@ class UserController { if (!bcrypt.compareSync(password, user.password)) throw new customError(`Wrong user password`, responseCodes.responses.passwords.invalid); const token = jwtutils.genToken(user); - return res.status(200).send(token); + return res.status(200).send({ id: user.id, token: token }); } async synchronize(req, res) { diff --git a/src/services/user.js b/src/services/user.js index f559832..af2be6d 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -6,9 +6,11 @@ import errorHandler from '../utils/pgerrorhandler.js'; class UserService { async create(username, password) { - await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, bcrypt.hashSync(password, 12)]).catch((e) => { + let result = (await db.query("INSERT INTO users (username, password) VALUES ($1, $2) RETURNING id", [username, bcrypt.hashSync(password, 12)]).catch((e) => { errorHandler(e, "user"); - }) + })).rows[0].id + + return result } async getByUsername(username) {