From e85db0f897b50d7df4a86d676bcfbe3764334634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=20Gramn=C3=A6s=20Tjernshaugen?= Date: Sat, 24 Aug 2024 12:03:47 +0200 Subject: [PATCH 1/2] Add feide groups to auth0 profile --- .../organisms/Navbar/ProfileMenu.tsx | 4 +-- .../components/views/ProfileView/index.tsx | 22 ++++++++++-- infra/auth0/js/fetchUserProfile.js | 36 ++++++++++++++----- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/apps/web/src/components/organisms/Navbar/ProfileMenu.tsx b/apps/web/src/components/organisms/Navbar/ProfileMenu.tsx index ba551d3a7..6defefc06 100644 --- a/apps/web/src/components/organisms/Navbar/ProfileMenu.tsx +++ b/apps/web/src/components/organisms/Navbar/ProfileMenu.tsx @@ -55,14 +55,14 @@ const InnerProfileMenu = () => { diff --git a/apps/web/src/components/views/ProfileView/index.tsx b/apps/web/src/components/views/ProfileView/index.tsx index 208bbb3db..7d393aeb9 100644 --- a/apps/web/src/components/views/ProfileView/index.tsx +++ b/apps/web/src/components/views/ProfileView/index.tsx @@ -1,8 +1,26 @@ import type { NextPage } from "next" import type { User } from "next-auth" +import {Button} from "../../../../../../packages/ui"; +import { Issuer } from "openid-client"; +import {oidcConfig} from "@/app/dataporten/study/callback/route"; +import {getAuthorizationUrl} from "@/utils/dataporten"; +import client from "@/api/sanity"; +import {cookies} from "next/headers"; -const ProfilePoster: NextPage<{ user: User }> = ({ user }) => { - return
ProfilePoster
+const ProfilePoster: NextPage<{ user: User }> = async ({ user }) => { + const authorizationUrl = await getAuthorizationUrl(); + + const groups = cookies().getAll().map(cookie => cookie.name); + return
+

+ Logg inn med FEIDE +

+
+ +
+      {JSON.stringify(groups, null, 2)}
+    
+
} export default ProfilePoster diff --git a/infra/auth0/js/fetchUserProfile.js b/infra/auth0/js/fetchUserProfile.js index a9e44d899..0270e550a 100644 --- a/infra/auth0/js/fetchUserProfile.js +++ b/infra/auth0/js/fetchUserProfile.js @@ -20,13 +20,33 @@ function fetchUserProfile(accessToken, ctx, callback) { } catch (jsonError) { return callback(new Error(body)); } - const profile = { - user_id: bodyParsed.sub, - email: bodyParsed.email, - "email_verified": bodyParsed.email_verified, - "name": bodyParsed.name, - }; - callback(null, profile); + + request.get("https://groups-api.dataporten.no/groups/me/groups?show_all=true", { + headers: { + 'Authorization': 'Bearer ' + accessToken, + } + }, (err, resp, body) => { + if (err) { + return callback(err); + } + if (resp.statusCode !== 200) { + return callback(new Error(body)); + } + let bodyParsedGroups; + try { + bodyParsedGroups = JSON.parse(body); + } catch (jsonError) { + return callback(new Error(body)); + } + const profile = { + user_id: bodyParsed.sub, + email: bodyParsed.email, + "email_verified": bodyParsed.email_verified, + "name": bodyParsed.name, + "feide-groups": bodyParsedGroups, + }; + callback(null, profile); + }); } ); - } \ No newline at end of file +} \ No newline at end of file From c2394b7d8ab65491865b7dce82ee13f35465890a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=20Gramn=C3=A6s=20Tjernshaugen?= Date: Sat, 24 Aug 2024 12:13:13 +0200 Subject: [PATCH 2/2] Remove test coide --- .../components/views/ProfileView/index.tsx | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/apps/web/src/components/views/ProfileView/index.tsx b/apps/web/src/components/views/ProfileView/index.tsx index 7d393aeb9..208bbb3db 100644 --- a/apps/web/src/components/views/ProfileView/index.tsx +++ b/apps/web/src/components/views/ProfileView/index.tsx @@ -1,26 +1,8 @@ import type { NextPage } from "next" import type { User } from "next-auth" -import {Button} from "../../../../../../packages/ui"; -import { Issuer } from "openid-client"; -import {oidcConfig} from "@/app/dataporten/study/callback/route"; -import {getAuthorizationUrl} from "@/utils/dataporten"; -import client from "@/api/sanity"; -import {cookies} from "next/headers"; -const ProfilePoster: NextPage<{ user: User }> = async ({ user }) => { - const authorizationUrl = await getAuthorizationUrl(); - - const groups = cookies().getAll().map(cookie => cookie.name); - return
-

- Logg inn med FEIDE -

-
- -
-      {JSON.stringify(groups, null, 2)}
-    
-
+const ProfilePoster: NextPage<{ user: User }> = ({ user }) => { + return
ProfilePoster
} export default ProfilePoster