Skip to content

Commit 7677e77

Browse files
committed
update
1 parent 0b6393b commit 7677e77

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

http/keycloak.http

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
@keycloakUrl=https://auth.unterrainer.info
2-
@realm=safeLearn
3-
@clientId=safeLearn
41
###
52
# @name POST OWN attributes (different URL) (needs no additional realm-management role)
63
POST {{keycloakUrl}}/realms/{{realm}}/account
74
Authorization: openid password env
85
Content-Type: application/json
96

107
{
11-
"firstName": "Tea",
12-
"lastName": "Cher",
13-
"email": "[email protected]",
148
"attributes": {
15-
"config": "custom value2",
9+
"firstName": "Tea",
10+
"lastName": "Cher",
11+
"config": "custom value1",
1612
"lastVisitedUrl": "https://safelearn.unterrainer.info"
1713
}
1814
}

middlewares/keycloak-middleware.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ export async function getUserAttributes(req, getAll = false) {
194194
return response.json();
195195
})
196196
.then((data) => {
197-
for(let key in data.attributes) {
198-
data.attributes[key] = data.attributes[key][0];
197+
for(let key in data) {
198+
data[key] = data[key][0];
199199
}
200200
if(getAll) {
201201
// Remove fields from the object that are not needed.
202202
let { userProfileMetadata, id, username, emailVerified, ...d} = data;
203203
return d;
204204
}
205-
return data.attributes;
205+
return data;
206206
})
207207
.catch((error) => {
208208
console.error("Error fetching current attributes:", error);
@@ -212,7 +212,7 @@ export async function getUserAttributes(req, getAll = false) {
212212
}
213213

214214
export async function setUserAttribute(req, attributeName, attributeValue) {
215-
if (!req || !req.user || !req.user.accessToken ||!req.user.keycloakConfig) {
215+
if (!req || !req.user || !req.user.accessToken || !req.user.keycloakConfig) {
216216
return null;
217217
}
218218
const keycloakConfig = req.user.keycloakConfig;
@@ -223,10 +223,12 @@ export async function setUserAttribute(req, attributeName, attributeValue) {
223223
// Fetch current user attributes
224224
const currentAttributes = await getUserAttributes(req, true);
225225

226-
// Merge current and new attributes
227-
const mas = { ...currentAttributes.attributes, [attributeName]: attributeValue };
228-
const mergedAttributes = { ...currentAttributes, attributes: mas };
229-
226+
// Merge current and new attributes at the root level
227+
let mergedAttributes = { ...currentAttributes, [attributeName]: attributeValue };
228+
mergedAttributes = {
229+
attributes: mergedAttributes
230+
}
231+
230232
const result = fetch(url, {
231233
method: "POST",
232234
headers: {

0 commit comments

Comments
 (0)