This commit is contained in:
Bright 2025-01-22 17:42:23 +07:00
parent dcc53befb2
commit 9f5f1af6e2
3 changed files with 123 additions and 82 deletions

View file

@ -114,6 +114,28 @@ export async function getUser(userId: string) {
return await res.json();
}
/**
* Get keycloak user by Username (citizenId)
*
* Client must have permission to manage realm's user
*
* @returns user if success, false otherwise.
*/
export async function getUserByUsername(citizenId: string) {
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?username=${citizenId}`, {
// prettier-ignore
headers: {
"authorization": `Bearer ${await getToken()}`,
"content-type": `application/json`,
},
}).catch((e) => console.log("Keycloak Error: ", e));
if (!res) return false;
if (!res.ok) {
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
}
return await res.json();
}
/**
* Get keycloak user list
*
@ -476,7 +498,7 @@ export async function getUserRoles(userId: string) {
*/
export async function addUserRoles(userId: string, roles: { id: string; name: string }[]) {
const res = await fetch(
`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/role-mappings/realm`,
`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`,
{
// prettier-ignore
headers: {