Skip to content

Commit 0214a91

Browse files
authored
feat: call the CIS2 userinfo endpoint (#1177)
1 parent 440e548 commit 0214a91

File tree

1 file changed

+22
-0
lines changed
  • application/CohortManager/src/Web/app/lib

1 file changed

+22
-0
lines changed

application/CohortManager/src/Web/app/lib/auth.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
9090
return isValidToken;
9191
},
9292
async jwt({ account, token, profile }) {
93+
if (account?.access_token) {
94+
try {
95+
const response = await fetch(
96+
`${process.env.AUTH_CIS2_ISSUER_URL}/openam/oauth2/realms/root/realms/NHSIdentity/realms/Healthcare/userinfo`,
97+
{
98+
method: "GET",
99+
headers: {
100+
Authorization: `Bearer ${account.access_token}`,
101+
},
102+
}
103+
);
104+
if (!response.ok) {
105+
throw new Error("Failed to call the userinfo endpoint from CIS2");
106+
}
107+
const userInfo: Profile = await response.json();
108+
token.profile = userInfo;
109+
} catch (error) {
110+
console.error("Error fetching user info:", error);
111+
return token;
112+
}
113+
}
114+
93115
// Handle test accounts in development
94116
if (
95117
process.env.NODE_ENV === "development" &&

0 commit comments

Comments
 (0)