fix(auth): error login on user or password is incorrect

This commit is contained in:
avicenan
2026-04-15 10:20:23 +07:00
parent 6a0fa980bd
commit 567089c03d
2 changed files with 74 additions and 70 deletions

View File

@ -21,18 +21,18 @@ const log: Logger<ILogObj> = new Logger({ name: "[UserController]", type: "prett
export class AuthController { export class AuthController {
static async login(req: Request, res: Response, next: NextFunction): Promise<Response> { static async login(req: Request, res: Response, next: NextFunction): Promise<Response> {
/* /*
#swagger.tags = ['Auth'] #swagger.tags = ['Auth']
#swagger.requestBody = { #swagger.requestBody = {
required: true, required: true,
content: { content: {
"application/json": { "application/json": {
schema: { schema: {
$ref: "#/components/schemas/login" $ref: "#/components/schemas/login"
} }
} }
} }
} }
*/ */
try { try {
const schema = Joi.object().keys({ const schema = Joi.object().keys({
@ -52,6 +52,10 @@ export class AuthController {
where: { username: param.username }, where: { username: param.username },
}) })
if (!user) {
return ReturnHelper.errorResponse(res, 401, 401, Language.lang.failed_access + ", username or password is incorrect")
}
if (user != null && user.roles != null && user.roles.length > 0 && user.checkIfPasswordMatch(param.password)) { if (user != null && user.roles != null && user.roles.length > 0 && user.checkIfPasswordMatch(param.password)) {
const { browser, device, os } = UAParser(req.get("User-Agent")) const { browser, device, os } = UAParser(req.get("User-Agent"))
@ -146,9 +150,9 @@ export class AuthController {
} }
if (user.roles == null || user.roles.length == 0) { if (user.roles == null || user.roles.length == 0) {
return ReturnHelper.errorResponse(res, 403, 401, Language.lang.failed_access + ", you don\t have any roles in this application") return ReturnHelper.errorResponse(res, 401, 401, Language.lang.failed_access + ", you don't have any roles in this application")
} else { } else {
return ReturnHelper.errorResponse(res, 403, 402, Language.lang.failed_access) return ReturnHelper.errorResponse(res, 401, 401, Language.lang.failed_access + ", username or password is incorrect")
} }
} catch (e: unknown) { } catch (e: unknown) {
log.error(e) log.error(e)
@ -248,14 +252,14 @@ export class AuthController {
static async logout(req: Request, res: Response, next: NextFunction): Promise<Response> { static async logout(req: Request, res: Response, next: NextFunction): Promise<Response> {
/* /*
#swagger.tags = ['Auth'] #swagger.tags = ['Auth']
#swagger.parameters['refresh_token'] = { #swagger.parameters['refresh_token'] = {
in: 'path', in: 'path',
description: 'Refresh Token', description: 'Refresh Token',
required: true, required: true,
type: 'string' type: 'string'
} }
*/ */
try { try {
const schema = Joi.object().keys({ const schema = Joi.object().keys({
@ -283,20 +287,20 @@ export class AuthController {
static async resetPassword(req: Request, res: Response, next: NextFunction): Promise<Response> { static async resetPassword(req: Request, res: Response, next: NextFunction): Promise<Response> {
/* /*
#swagger.tags = ['Auth'] #swagger.tags = ['Auth']
#swagger.parameters['email'] = { #swagger.parameters['email'] = {
in: 'path', in: 'path',
description: 'Email', description: 'Email',
required: true, required: true,
type: 'string' type: 'string'
} }
#swagger.parameters['application'] = { #swagger.parameters['application'] = {
in: 'path', in: 'path',
description: 'Application', description: 'Application',
required: true, required: true,
type: 'string' type: 'string'
} }
*/ */
try { try {
const schema = Joi.object().keys({ const schema = Joi.object().keys({
@ -357,20 +361,20 @@ export class AuthController {
static async renewToken(req: Request, res: Response, next: NextFunction): Promise<Response> { static async renewToken(req: Request, res: Response, next: NextFunction): Promise<Response> {
/* /*
#swagger.tags = ['Auth'] #swagger.tags = ['Auth']
#swagger.parameters['refresh_token'] = { #swagger.parameters['refresh_token'] = {
in: 'path', in: 'path',
description: 'Refresh Token', description: 'Refresh Token',
required: true, required: true,
type: 'string' type: 'string'
} }
#swagger.parameters['application'] = { #swagger.parameters['application'] = {
in: 'path', in: 'path',
description: 'Application', description: 'Application',
required: true, required: true,
type: 'string' type: 'string'
} }
*/ */
try { try {
const schema = Joi.object().keys({ const schema = Joi.object().keys({
refresh_token: Joi.string().uuid().required().label("Refresh Token"), refresh_token: Joi.string().uuid().required().label("Refresh Token"),
@ -445,27 +449,27 @@ export class AuthController {
static async updatePassword(req: Request, res: Response, next: NextFunction): Promise<Response> { static async updatePassword(req: Request, res: Response, next: NextFunction): Promise<Response> {
/* /*
#swagger.tags = ['Auth'] #swagger.tags = ['Auth']
#swagger.parameters['token'] = { #swagger.parameters['token'] = {
in: 'path', in: 'path',
description: 'Token', description: 'Token',
required: true, required: true,
type: 'string' type: 'string'
} }
#swagger.requestBody = { #swagger.requestBody = {
required: true, required: true,
description: "This action will effect to user related token", description: "This action will effect to user related token",
content: { content: {
"application/json": { "application/json": {
schema: { schema: {
$ref: "#/components/schemas/user_password" $ref: "#/components/schemas/user_password"
} }
} }
} }
} }
*/ */
try { try {
const schema = Joi.object().keys({ const schema = Joi.object().keys({

View File

@ -18,7 +18,7 @@ const doc = {
login: { login: {
$username: "admin", $username: "admin",
$password: "12345aA!", $password: "12345aA!",
$application: "hrms", $application: "saude",
}, },
user: { user: {
$employee_id: "EMP001", $employee_id: "EMP001",