Skip to content

Commit

Permalink
CCM-5100: Fetch and display id token payload on status page
Browse files Browse the repository at this point in the history
  • Loading branch information
m-houston committed Jul 2, 2024
1 parent ae7793b commit 44a6ae3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/signout/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <>
<h1>Sign out</h1>
<p>You are currently logged in as <strong>{attributes?.email}</strong></p>
<p>You are currently logged in as <strong>{idToken?.email?.toString()}</strong></p>
<Logout />
</>
}
8 changes: 4 additions & 4 deletions app/status/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <>
<h1>Hello {currentUser?.username}</h1>
<h2>Login details</h2>
<pre>{JSON.stringify(currentUser?.signInDetails, null, ' ')}</pre>
<h2>Attributes</h2>
<pre>{JSON.stringify(attributes, null, ' ')}</pre>
<pre>{JSON.stringify(currentUser, null, ' ')}</pre>
<h2>Identity details</h2>
<pre>{JSON.stringify(idToken, null, ' ')}</pre>
<h2>Redirect details</h2>
<pre>{JSON.stringify({ redirectPath })}</pre>
</>
Expand Down
21 changes: 17 additions & 4 deletions components/LoginStatus.tsx
Original file line number Diff line number Diff line change
@@ -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<JwtPayload | undefined>();
useEffect(() => {
(async () => {
const session = await fetchAuthSession();
setIdToken(session.tokens?.idToken?.payload)
})().catch(console.error);
}, [authStatus]);

switch (authStatus) {
case 'authenticated':
return <Header.NavItem href="/auth/signout">
Sign out
</Header.NavItem>;
return [
<Header.ServiceName>{idToken?.email?.toString() || ''}</Header.ServiceName>,
<Header.NavItem href="/auth/signout">
Sign out
</Header.NavItem>
];
case 'unauthenticated':
return <Header.NavItem href={`/auth/?redirect=${location.pathname}`}>
Sign in
Expand Down
9 changes: 4 additions & 5 deletions utils/amplify-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 44a6ae3

Please sign in to comment.