now sending userid on login and register

This commit is contained in:
leca 2024-11-03 02:41:13 +03:00
parent 0b32306f84
commit 69c2255614
2 changed files with 7 additions and 5 deletions

View File

@ -15,10 +15,10 @@ class UserController {
async register(req, res) { async register(req, res) {
const { username, password } = req.body; 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`); 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) { 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); if (!bcrypt.compareSync(password, user.password)) throw new customError(`Wrong user password`, responseCodes.responses.passwords.invalid);
const token = jwtutils.genToken(user); 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) { async synchronize(req, res) {

View File

@ -6,9 +6,11 @@ import errorHandler from '../utils/pgerrorhandler.js';
class UserService { class UserService {
async create(username, password) { 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"); errorHandler(e, "user");
}) })).rows[0].id
return result
} }
async getByUsername(username) { async getByUsername(username) {