feat: delete user

This commit is contained in:
Methapon2001 2024-04-10 13:37:33 +07:00
parent 32127ae804
commit 7e6325d359
2 changed files with 30 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import { Body, Controller, Delete, Get, Path, Post, Put, Route, Security, Tags }
import {
addUserRoles,
createUser,
deleteUser,
editUser,
getRoles,
removeUserRoles,
@ -30,6 +31,11 @@ export class KeycloakController extends Controller {
return await editUser(userId, body);
}
@Delete("user/{userId}")
async deleteUser(@Path() userId: string) {
return await deleteUser(userId);
}
@Get("role")
async getRole() {
const role = await getRoles();

View file

@ -92,7 +92,7 @@ export async function createUser(username: string, password: string, opts?: Reco
}
/**
* Update keycloak user by given username and password with roles
* Update keycloak user by uuid
*
* Client must have permission to manage realm's user
*
@ -125,6 +125,29 @@ export async function editUser(userId: string, opts: Record<string, any>) {
return id || true;
}
/**
* Delete keycloak user by uuid
*
* Client must have permission to manage realm's user
*
* @returns user uuid or true if success, false otherwise.
*/
export async function deleteUser(userId: string) {
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, {
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"content-type": `application/json`,
},
method: "DELETE",
}).catch((e) => console.log("Keycloak Error: ", e));
if (!res) return false;
if (!res.ok) {
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
}
}
/**
* Get roles list or specific role data
*