From 44a6ae3787d563e8ee0e37d5613b1c13d35a57e4 Mon Sep 17 00:00:00 2001 From: Mike Houston Date: Tue, 2 Jul 2024 16:23:41 +0100 Subject: [PATCH] CCM-5100: Fetch and display id token payload on status page --- app/signout/page.tsx | 4 ++-- app/status/page.tsx | 8 ++++---- components/LoginStatus.tsx | 21 +++++++++++++++++---- utils/amplify-utils.ts | 9 ++++----- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/signout/page.tsx b/app/signout/page.tsx index b13f2b8..78129d3 100644 --- a/app/signout/page.tsx +++ b/app/signout/page.tsx @@ -5,14 +5,14 @@ import { redirect } from 'next/navigation'; export default async function Page() { - const { currentUser, attributes } = await AuthGetCurrentUserServer() ?? {}; + const { currentUser, idToken } = await AuthGetCurrentUserServer() ?? {}; if (!currentUser) { redirect('/'); } return <>

Sign out

-

You are currently logged in as {attributes?.email}

+

You are currently logged in as {idToken?.email?.toString()}

} diff --git a/app/status/page.tsx b/app/status/page.tsx index d148c51..1272600 100644 --- a/app/status/page.tsx +++ b/app/status/page.tsx @@ -5,15 +5,15 @@ export default async function Page({ searchParams }: { searchParams: { [key: string]: string | string[] | undefined } }) { - const { currentUser, attributes } = await AuthGetCurrentUserServer() ?? {}; + const { currentUser, idToken } = await AuthGetCurrentUserServer() ?? {}; const redirectPath = [searchParams.redirect].flat().pop() || '/'; return <>

Hello {currentUser?.username}

Login details

-
{JSON.stringify(currentUser?.signInDetails, null, '  ')}
-

Attributes

-
{JSON.stringify(attributes, null, '  ')}
+
{JSON.stringify(currentUser, null, '  ')}
+

Identity details

+
{JSON.stringify(idToken, null, '  ')}

Redirect details

{JSON.stringify({ redirectPath })}
diff --git a/components/LoginStatus.tsx b/components/LoginStatus.tsx index 771ec0f..be381f4 100644 --- a/components/LoginStatus.tsx +++ b/components/LoginStatus.tsx @@ -1,16 +1,29 @@ 'use client'; import { Header } from 'nhsuk-react-components'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { useAuthenticator } from '@aws-amplify/ui-react'; +import { fetchAuthSession } from 'aws-amplify/auth'; +import { JwtPayload } from 'aws-jwt-verify/jwt-model'; export default function LoginStatus() { const { authStatus } = useAuthenticator(); + const [idToken, setIdToken] = useState(); + useEffect(() => { + (async () => { + const session = await fetchAuthSession(); + setIdToken(session.tokens?.idToken?.payload) + })().catch(console.error); + }, [authStatus]); + switch (authStatus) { case 'authenticated': - return - Sign out - ; + return [ + {idToken?.email?.toString() || ''}, + + Sign out + + ]; case 'unauthenticated': return Sign in diff --git a/utils/amplify-utils.ts b/utils/amplify-utils.ts index d29a869..cff9923 100644 --- a/utils/amplify-utils.ts +++ b/utils/amplify-utils.ts @@ -16,11 +16,10 @@ export async function AuthGetCurrentUserServer() { const currentUser = await getCurrentUser(contextSpec); console.log({ currentUser }); - // const session = await fetchAuthSession(contextSpec); - // console.log({ session }) - const attributes = await fetchUserAttributes(contextSpec); - console.log({ attributes }); - return { currentUser, attributes }; + const session = await fetchAuthSession(contextSpec); + const idToken = session.tokens?.idToken?.payload; + console.log({ idToken }) + return { currentUser, idToken }; } }); } catch (error) {