generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CCM-5100: Improved handling of redirects
- Loading branch information
Showing
9 changed files
with
163 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
import React from 'react'; | ||
import Redirect from '../components/Redirect'; | ||
'use client'; | ||
|
||
export default async function Page({ searchParams }: { | ||
import React, { useEffect, useState } from 'react'; | ||
import { Hub } from '@aws-amplify/core'; | ||
import Login from '../components/Login'; | ||
|
||
export default function Page({ searchParams }: { | ||
searchParams: { [key: string]: string | string[] | undefined } | ||
}) { | ||
|
||
const redirectPath = [searchParams.redirect].flat().pop() || '/'; | ||
if (!redirectPath.match(/^\/[a-z/]*$/)) { | ||
const [redirect, setRedirect] = useState<string | undefined>(); | ||
|
||
useEffect(() => { | ||
return Hub.listen('auth', ({ payload }) => { | ||
switch (payload.event) { | ||
case 'customOAuthState': | ||
try { | ||
const { redirectPath } = JSON.parse(payload.data); | ||
setRedirect(redirectPath); | ||
} catch (err) { | ||
console.error(err); | ||
setRedirect('Invalid redirect path'); | ||
} | ||
break; | ||
} | ||
}); | ||
}, []); | ||
|
||
let redirectPath = redirect || [searchParams.redirect].flat().pop() || '/'; | ||
if (redirectPath === '/auth') redirectPath = '/'; | ||
if (redirectPath && !redirectPath.match(/^\/[a-z/]*$/)) { | ||
return <h2>Invalid redirect path</h2>; | ||
} | ||
|
||
return <> | ||
<Redirect path={redirectPath} /> | ||
</> | ||
return <Login redirectPath={redirectPath} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import { AuthGetCurrentUserServer } from '../../utils/amplify-utils'; | ||
import Logout from '../../components/Logout'; | ||
import { redirect } from 'next/navigation'; | ||
|
||
export default async function Page() { | ||
|
||
const { currentUser, attributes } = await AuthGetCurrentUserServer() ?? {}; | ||
if (!currentUser) { | ||
redirect('/'); | ||
} | ||
|
||
return <> | ||
<h1>Sign out</h1> | ||
<p>You are currently logged in as <strong>{attributes?.email}</strong></p> | ||
<Logout /> | ||
</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use client'; | ||
|
||
import { Heading, useTheme, withAuthenticator } from '@aws-amplify/ui-react'; | ||
import { AuthUser } from 'aws-amplify/auth'; | ||
import React, { useEffect } from 'react'; | ||
import { Amplify } from 'aws-amplify'; | ||
import { signInWithRedirect } from '@aws-amplify/auth'; | ||
import { Button } from 'nhsuk-react-components'; | ||
import { | ||
DefaultComponents | ||
} from '@aws-amplify/ui-react/dist/types/components/Authenticator/hooks/useCustomComponents/defaultComponents'; | ||
|
||
function auth0Login(redirectPath: string) { | ||
console.log(Amplify.getConfig()); | ||
signInWithRedirect({ | ||
provider: { | ||
custom: 'Auth0' | ||
}, | ||
customState: JSON.stringify({ redirectPath }) | ||
}).catch(console.error); | ||
} | ||
|
||
function components(redirectPath: string): DefaultComponents { | ||
return { | ||
|
||
SignIn: { | ||
Header() { | ||
const { tokens } = useTheme(); | ||
|
||
return (<> | ||
<Heading | ||
padding={`${tokens.space.xl} 0 0 ${tokens.space.xl}`} | ||
level={3} | ||
> | ||
Sign in to your account | ||
</Heading> | ||
<div className="login-oidc" | ||
style={{ | ||
position: 'relative', | ||
padding: `${tokens.space.xl} ${tokens.space.xl} 0 ${tokens.space.xl}` }}> | ||
<Button | ||
className="login-oidc__button" | ||
style={{width: '100%'}} | ||
onClick={() => auth0Login(redirectPath)}>Log in with Auth0</Button> | ||
</div> | ||
<div style={{textAlign:'center'}}> | ||
or | ||
</div> | ||
</> | ||
); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
function Login({ user, redirectPath }: { user?: AuthUser, redirectPath?: string }) { | ||
useEffect(() => { | ||
if (user && redirectPath) { | ||
location.href = redirectPath; | ||
} | ||
}, [user, redirectPath]); | ||
if (redirectPath && user) { | ||
return <h3>Redirecting to <code><a href={redirectPath}>{redirectPath}</a></code></h3>; | ||
} | ||
return null; | ||
} | ||
|
||
export default (props: { redirectPath?: string; user?: AuthUser }) => { | ||
const { redirectPath } = props; | ||
return withAuthenticator(Login, { | ||
variation: 'default', | ||
hideSignUp: true, | ||
components: components(redirectPath || '/') | ||
})(props); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client'; | ||
|
||
import { Header } from 'nhsuk-react-components'; | ||
import React from 'react'; | ||
import { useAuthenticator } from '@aws-amplify/ui-react'; | ||
|
||
export default function LoginStatus() { | ||
const { authStatus } = useAuthenticator(); | ||
switch (authStatus) { | ||
case 'authenticated': | ||
return <Header.NavItem href="/auth/signout"> | ||
Sign out | ||
</Header.NavItem>; | ||
case 'unauthenticated': | ||
return <Header.NavItem href={`/auth/?redirect=${location.pathname}`}> | ||
Sign in | ||
</Header.NavItem>; | ||
default: | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,15 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/navigation'; | ||
import { useAuthenticator } from '@aws-amplify/ui-react'; | ||
import { Button } from 'nhsuk-react-components'; | ||
|
||
export default function Logout() { | ||
const router = useRouter(); | ||
const { user, signOut } = useAuthenticator(); | ||
const { authStatus, signOut } = useAuthenticator(); | ||
|
||
async function handleSignOut() { | ||
signOut(); | ||
location.href = '/'; | ||
} | ||
|
||
return user ? ( | ||
<button | ||
onClick={handleSignOut} | ||
className="px-2 bg-white text-black" | ||
> | ||
Sign out | ||
</button> | ||
) : []; | ||
return <Button onClick={handleSignOut}>Sign out</Button>; | ||
} |
This file was deleted.
Oops, something went wrong.