Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
guFalcon committed Jul 12, 2024
1 parent 0b6393b commit 7677e77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
10 changes: 3 additions & 7 deletions http/keycloak.http
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
@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
Authorization: openid password env
Content-Type: application/json

{
"firstName": "Tea",
"lastName": "Cher",
"email": "[email protected]",
"attributes": {
"config": "custom value2",
"firstName": "Tea",
"lastName": "Cher",
"config": "custom value1",
"lastVisitedUrl": "https://safelearn.unterrainer.info"
}
}
Expand Down
18 changes: 10 additions & 8 deletions middlewares/keycloak-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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: {
Expand Down

0 comments on commit 7677e77

Please sign in to comment.