Skip to content

Commit ecc725b

Browse files
committed
feat(userinfo middleware): cache authentication info only for requests other than /auth
1 parent b3935c1 commit ecc725b

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

middleware/userinfo.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const userinfo = createMiddleware(async (c, next) => {
3030
// Either fetch cached user info from Valkey else
3131
// fetch user info from Keycloak
3232
const cachedKeycloakUserInfo = await runCachedQueries(
33-
`userinfo_middleware_${bearerToken}`,
33+
`user_info_middleware_${bearerToken}`,
3434
async () => {
3535
const userInfo = await fetchUserInfo(bearerToken!);
3636
return JSON.stringify(userInfo);
@@ -41,23 +41,27 @@ export const userinfo = createMiddleware(async (c, next) => {
4141
// Set userinfo data for the current request
4242
c.set("userinfo", cachedKeycloakUserInfo);
4343

44-
// Either fetch cached authentication info from Valkey else
45-
// fetch authentication info from the database
46-
const cachedAuthenticationInfo = await runCachedQueries(
47-
`authenticationinfo_middleware_${bearerToken}`,
48-
async () => {
49-
const keycloakUserInfo = JSON.parse(c.get("userinfo"));
44+
if (c.req.path != "/auth") {
45+
// Either fetch cached authentication info from Valkey else
46+
// fetch authentication info from the database
47+
const cachedAuthenticationInfo = await runCachedQueries(
48+
`authentication_info_middleware_${bearerToken}`,
49+
async () => {
50+
const keycloakUserInfo = JSON.parse(c.get("userinfo"));
5051

51-
const authenticationInfo = await selectOneAuthenticationInfo(
52-
keycloakUserInfo["sub"],
53-
);
54-
return JSON.stringify(authenticationInfo);
55-
},
56-
60 * 5,
57-
);
52+
const authenticationInfo = await selectOneAuthenticationInfo(
53+
keycloakUserInfo["sub"],
54+
);
55+
return authenticationInfo == null
56+
? ""
57+
: JSON.stringify(authenticationInfo);
58+
},
59+
60 * 5,
60+
);
5861

59-
// Set AuthenticationInfo data for the current request
60-
c.set("authenticationinfo", cachedAuthenticationInfo);
62+
// Set AuthenticationInfo data for the current request
63+
c.set("authenticationinfo", cachedAuthenticationInfo);
64+
}
6165

6266
// Next
6367
await next();

0 commit comments

Comments
 (0)