From 7677e77b7aed835f9eac38df3ea99c8ed88d08a8 Mon Sep 17 00:00:00 2001 From: guFalcon Date: Fri, 12 Jul 2024 15:10:44 +0200 Subject: [PATCH] update --- http/keycloak.http | 10 +++------- middlewares/keycloak-middleware.js | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/http/keycloak.http b/http/keycloak.http index e2e431d..7bc218c 100644 --- a/http/keycloak.http +++ b/http/keycloak.http @@ -1,6 +1,3 @@ -@keycloakUrl=https://auth.unterrainer.info -@realm=safeLearn -@clientId=safeLearn ### # @name POST OWN attributes (different URL) (needs no additional realm-management role) POST {{keycloakUrl}}/realms/{{realm}}/account @@ -8,11 +5,10 @@ Authorization: openid password env Content-Type: application/json { - "firstName": "Tea", - "lastName": "Cher", - "email": "teacher@unterrainer.info", "attributes": { - "config": "custom value2", + "firstName": "Tea", + "lastName": "Cher", + "config": "custom value1", "lastVisitedUrl": "https://safelearn.unterrainer.info" } } diff --git a/middlewares/keycloak-middleware.js b/middlewares/keycloak-middleware.js index 0d7059e..11a3077 100644 --- a/middlewares/keycloak-middleware.js +++ b/middlewares/keycloak-middleware.js @@ -194,15 +194,15 @@ export async function getUserAttributes(req, getAll = false) { return response.json(); }) .then((data) => { - for(let key in data.attributes) { - data.attributes[key] = data.attributes[key][0]; + for(let key in data) { + data[key] = data[key][0]; } if(getAll) { // Remove fields from the object that are not needed. let { userProfileMetadata, id, username, emailVerified, ...d} = data; return d; } - return data.attributes; + return data; }) .catch((error) => { console.error("Error fetching current attributes:", error); @@ -212,7 +212,7 @@ export async function getUserAttributes(req, getAll = false) { } export async function setUserAttribute(req, attributeName, attributeValue) { - if (!req || !req.user || !req.user.accessToken ||!req.user.keycloakConfig) { + if (!req || !req.user || !req.user.accessToken || !req.user.keycloakConfig) { return null; } const keycloakConfig = req.user.keycloakConfig; @@ -223,10 +223,12 @@ export async function setUserAttribute(req, attributeName, attributeValue) { // Fetch current user attributes const currentAttributes = await getUserAttributes(req, true); - // Merge current and new attributes - const mas = { ...currentAttributes.attributes, [attributeName]: attributeValue }; - const mergedAttributes = { ...currentAttributes, attributes: mas }; - + // Merge current and new attributes at the root level + let mergedAttributes = { ...currentAttributes, [attributeName]: attributeValue }; + mergedAttributes = { + attributes: mergedAttributes + } + const result = fetch(url, { method: "POST", headers: {