feat: add update user data endpoint
This commit is contained in:
parent
6899c9823a
commit
addd4caa63
2 changed files with 51 additions and 3 deletions
|
|
@ -91,6 +91,40 @@ export async function createUser(username: string, password: string, opts?: Reco
|
|||
return id || true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update keycloak user by given username and password with roles
|
||||
*
|
||||
* Client must have permission to manage realm's user
|
||||
*
|
||||
* @returns user uuid or true if success, false otherwise.
|
||||
*/
|
||||
export async function editUser(userId: string, opts: Record<string, any>) {
|
||||
const { password, ...rest } = opts;
|
||||
|
||||
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, {
|
||||
// prettier-ignore
|
||||
headers: {
|
||||
"authorization": `Bearer ${await getToken()}`,
|
||||
"content-type": `application/json`,
|
||||
},
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
enabled: true,
|
||||
credentials: (password && [{ type: "password", value: opts?.password }]) || undefined,
|
||||
...rest,
|
||||
}),
|
||||
}).catch((e) => console.log("Keycloak Error: ", e));
|
||||
|
||||
if (!res) return false;
|
||||
if (!res.ok) {
|
||||
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
|
||||
}
|
||||
|
||||
const path = res.headers.get("Location");
|
||||
const id = path?.split("/").at(-1);
|
||||
return id || true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get roles list or specific role data
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue