diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 15394982..c8557eba 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -30,7 +30,7 @@ import { getRoleMappings, getUserCount, enableStatus, - getUserByUsername + getUserByUsername, } from "../keycloak"; import { AppDataSource } from "../database/data-source"; import { Profile } from "../entities/Profile"; @@ -97,15 +97,21 @@ export class KeycloakController extends Controller { profileId?: string; }, ) { - const userId = await createUser(body.username, body.password, { - firstName: body.firstName, - lastName: body.lastName, - // email: body.email, - }); - - if (typeof userId !== "string") { - throw new Error(userId.errorMessage); + const checkUser = await getUserByUsername(body.username); + let userId: any = ""; + if (checkUser.length == 0) { + userId = await createUser(body.username, body.password, { + firstName: body.firstName, + lastName: body.lastName, + // email: body.email, + }); + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + } else { + userId = checkUser[0].id; } + const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); const result = await addUserRoles( @@ -709,7 +715,7 @@ export class KeycloakController extends Controller { roles: rolesData, }; - return userDataWithRoles + return userDataWithRoles; } @Put("user/{userId}/enableStatus/{status}") //#log? diff --git a/src/keycloak/index.ts b/src/keycloak/index.ts index 23d4426c..1369e307 100644 --- a/src/keycloak/index.ts +++ b/src/keycloak/index.ts @@ -227,14 +227,7 @@ export async function getUserListOrg(first = "", max = "", search = "", userIds: } export async function getUserCountOrg(first = "", max = "", search = "", userIds: string[] = []) { - console.log(userIds); const userIdsParam = userIds.join(","); - console.log(userIdsParam); - console.log("xxxxxxxxxxxxxxxxx"); - console.log( - `${KC_URL}/admin/realms/${KC_REALMS}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam && userIdsParam != "" ? `&id=${userIdsParam}` : ""}`, - ); - console.log("aaaaaaaaaaaaaaaaaa"); const res = await fetch( `${KC_URL}/admin/realms/${KC_REALMS}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam && userIdsParam != "" ? `&id=${userIdsParam}` : ""}`, { @@ -497,18 +490,15 @@ export async function getUserRoles(userId: string) { * @returns true if success, false otherwise. */ export async function addUserRoles(userId: string, roles: { id: string; name: string }[]) { - const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, - { - // prettier-ignore - headers: { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, { + // prettier-ignore + headers: { "authorization": `Bearer ${await getToken()}`, "content-type": `application/json`, }, - method: "POST", - body: JSON.stringify(roles), - }, - ).catch((e) => console.log(e)); + method: "POST", + body: JSON.stringify(roles), + }).catch((e) => console.log(e)); if (!res) return false; if (!res.ok) {