From 44612b0050a90a373766d95eba167eb703d5e70f Mon Sep 17 00:00:00 2001 From: Abdul Rahman Reza Date: Sat, 30 Nov 2024 23:50:25 +0700 Subject: [PATCH] update auth --- src/controllers/auth.ts | 76 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index 5b9f039..5da23c7 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -11,7 +11,7 @@ import dayjs from 'dayjs' import jwt from 'jsonwebtoken'; import fs from 'fs'; import { v4 as uuidv4 } from 'uuid'; -import { EmailBody, User, UserRefreshToken, UserRole } from "entity"; +import { EmailBody, Menu, User, UserRefreshToken, UserRole } from "entity"; import axios from 'axios'; import CommonHelper from "../helpers/common"; @@ -86,8 +86,11 @@ export class AuthController { delete user.password; + + const data = { user, + roles_list: await AuthController.getListRole(userRole.roles), token: { access_token: token, refresh_token: refresh_token.refresh_token @@ -106,6 +109,68 @@ export class AuthController { } } + static async getListRole(roles: any): Promise { + const repoMenu = OrmHelper.DB.getRepository(Menu); + + const whereAttr = "id in (:id_menu)"; + const whereVal = roles; + + const res_list = repoMenu.createQueryBuilder() + .where(whereAttr, whereVal) + .andWhere('id_parent is null') + .orderBy('order', 'ASC'); + + const res_list_all = await repoMenu.createQueryBuilder().andWhere('id_parent is not null').getMany(); + let list_data_final = await res_list.getMany(); + + let list_child: any = {}; + for (let p of res_list_all) { + if (!list_child[p.id_parent]) { + list_child[p.id_parent] = []; + } + + list_child[p.id_parent].push(p); + } + + for (let f of list_data_final) { + if (list_child[f.id]) { + f.children = list_child[f.id]; + + for (let g of f.children) { + if (list_child[g.id]) { + g.children = list_child[g.id]; + + for (let h of g.children) { + if (list_child[h.id]) { + h.children = list_child[h.id]; + + for (let i of h.children) { + if (list_child[i.id]) { + i.children = list_child[i.id]; + + for (let j of i.children) { + if (list_child[j.id]) { + j.children = list_child[j.id]; + + for (let k of j.children) { + if (list_child[k.id]) { + k.children = list_child[k.id]; + } + } + } + } + } + } + } + } + } + } + } + } + + return list_data_final; + } + static async logout(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['Auth'] @@ -250,7 +315,10 @@ export class AuthController { { algorithm: 'RS256' } ); - return ReturnHelper.successResponseAny(res, 200, Language.lang.success, { token }); + return ReturnHelper.successResponseAny(res, 200, Language.lang.success, { + token, + roles_list: await AuthController.getListRole(userRole.roles) + }); } else { return ReturnHelper.errorResponse(res, 404, 401, Language.lang.failed, ""); } @@ -265,14 +333,14 @@ export class AuthController { static async updatePassword(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['Auth'] - + #swagger.parameters['token'] = { in: 'path', description: 'Token', required: true, type: 'string' } - + #swagger.requestBody = { required: true, description: "This action will effect to user related token",