diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index 2dca6b6..6f76ee6 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -1,4 +1,5 @@ -import { Request, Response, NextFunction, Application } from "express"; +import { Response, NextFunction, Application } from "express"; +import { Request } from 'express-jwt' import { ReturnHelper } from "../helpers/express/return"; import { OrmHelper } from "../helpers/orm"; import { User } from "../entity/users"; @@ -233,4 +234,56 @@ export class AuthController { return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed, err.message); } } + + static async updatePassword(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['Auth'] + + #swagger.requestBody = { + required: true, + description: "This action will effect to user related token", + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/user_password" + } + } + } + } + */ + + try { + const schema = Joi.object().keys({ + id: Joi.string().uuid().required().label('ID'), + password: Joi.string().required().label('Password'), + retype_password: Joi.ref('password'), + }); + + req.body.id = req.auth.data.id; + + const param: User = await schema.validateAsync(req.body); + + const userRepository = OrmHelper.DB.getRepository(User); + + const data = await userRepository.findOneBy({ id: param.id }); + + if (data != null) { + if (param.password) { + data.password = param.password + data.hashPassword(); + } + + await userRepository.save(data); + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_update, data); + } else { + return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed_not_found, ""); + } + } catch (e: unknown) { + log.error(e); + const err = e as Error; + + return ReturnHelper.errorResponse(res, 500, 402, Language.lang.failed_update, err.message); + } + } } \ No newline at end of file