setup auth middleware and sync code
This commit is contained in:
parent
e76e361981
commit
a487b73c3b
5 changed files with 684 additions and 0 deletions
|
|
@ -772,6 +772,70 @@ export async function changeUserPassword(userId: string, newPassword: string) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user attributes in Keycloak
|
||||
*
|
||||
* @param userId - Keycloak user ID
|
||||
* @param attributes - Object containing attribute names and their values (as arrays)
|
||||
* @returns true if success, false otherwise
|
||||
*/
|
||||
export async function updateUserAttributes(
|
||||
userId: string,
|
||||
attributes: Record<string, string[]>,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
// Get existing user data to preserve other attributes
|
||||
const existingUser = await getUser(userId);
|
||||
|
||||
if (!existingUser) {
|
||||
console.error(`User ${userId} not found in Keycloak`);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Merge existing attributes with new attributes
|
||||
// Keycloak requires id to be present in the payload
|
||||
const updatedAttributes = {
|
||||
id: existingUser.id,
|
||||
enabled: existingUser.enabled ?? true,
|
||||
attributes: {
|
||||
...(existingUser.attributes || {}),
|
||||
...attributes,
|
||||
},
|
||||
};
|
||||
|
||||
console.log(`[updateUserAttributes] Sending to Keycloak:`, JSON.stringify(updatedAttributes, null, 2));
|
||||
|
||||
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
method: "PUT",
|
||||
body: JSON.stringify(updatedAttributes),
|
||||
}).catch((e) => {
|
||||
console.error(`[updateUserAttributes] Network error:`, e);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!res) {
|
||||
console.error(`[updateUserAttributes] No response from Keycloak`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
console.error(`[updateUserAttributes] Keycloak Error (${res.status}):`, errorText);
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(`[updateUserAttributes] Successfully updated attributes for user ${userId}`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`[updateUserAttributes] Error updating attributes for user ${userId}:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Function to reset password
|
||||
export async function resetPassword(username: string) {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue