added endpoints for changing password and username
This commit is contained in:
parent
afa1edeb8d
commit
02b1a572d1
|
@ -7,6 +7,7 @@ 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';
|
import customError from '../response/customError.js';
|
||||||
|
import jwt from 'jsonwebtoken';
|
||||||
|
|
||||||
const TAG = "/controllers/userjs"
|
const TAG = "/controllers/userjs"
|
||||||
|
|
||||||
|
@ -40,6 +41,23 @@ class UserController {
|
||||||
|
|
||||||
return res.status(200).send(result);
|
return res.status(200).send(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async changeUsername(req, res) {
|
||||||
|
const userId = jwt.decode(req.headers.authorization.split(' ')[1]).login.id
|
||||||
|
const { username } = req.body;
|
||||||
|
|
||||||
|
await UserService.changeUsername(userId, username);
|
||||||
|
|
||||||
|
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok))
|
||||||
|
}
|
||||||
|
|
||||||
|
async changePassword(req, res) {
|
||||||
|
const userId = jwt.decode(req.headers.authorization.split(' ')[1]).login.id
|
||||||
|
const { password } = req.body;
|
||||||
|
|
||||||
|
await UserService.changePassword(userId, password);
|
||||||
|
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new UserController()
|
export default new UserController()
|
|
@ -8,5 +8,7 @@ const UserRouter = new Router();
|
||||||
UserRouter.post('/register', tryHandler(auth.requireUsername), tryHandler(auth.requirePassword), tryHandler(UserController.register));
|
UserRouter.post('/register', tryHandler(auth.requireUsername), tryHandler(auth.requirePassword), tryHandler(UserController.register));
|
||||||
UserRouter.post('/login', tryHandler(auth.requireUsername), tryHandler(auth.requirePassword), tryHandler(UserController.login));
|
UserRouter.post('/login', tryHandler(auth.requireUsername), tryHandler(auth.requirePassword), tryHandler(UserController.login));
|
||||||
UserRouter.get('/synchronize/:groupId', tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(UserController.synchronize));
|
UserRouter.get('/synchronize/:groupId', tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(UserController.synchronize));
|
||||||
|
UserRouter.post('/changeUsername', tryHandler(auth.authenticate), tryHandler(auth.requireUsername), tryHandler(UserController.changeUsername));
|
||||||
|
UserRouter.post('/changePassword', tryHandler(auth.authenticate), tryHandler(auth.requirePassword), tryHandler(UserController.changePassword));
|
||||||
|
|
||||||
export default UserRouter;
|
export default UserRouter;
|
|
@ -31,6 +31,14 @@ class UserService {
|
||||||
if (await (this.isInGroup(userId, groupId))) throw new customError(`joinGroup user already in group`, responseCodes.responses.user.already_in_group);
|
if (await (this.isInGroup(userId, groupId))) throw new customError(`joinGroup user already in group`, responseCodes.responses.user.already_in_group);
|
||||||
await db.query("UPDATE Users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
|
await db.query("UPDATE Users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async changeUsername(userId, username) {
|
||||||
|
await db.query("UPDATE users SET username = $1 WHERE id = $2", [username, userId]).catch(e => errorHandler(e, "user"));
|
||||||
|
}
|
||||||
|
|
||||||
|
async changePassword(userId, password) {
|
||||||
|
await db.query("UPDATE users SET password = $1 WHERE id = $2", [bcrypt.hashSync(password, 12), userId])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new UserService();
|
export default new UserService();
|
Loading…
Reference in New Issue