Skip to content

Commit 6c959b0

Browse files
authored
Handle TOKEN_EXPIRED from Auth API (#43)
* Clear cookies and redirect on TOKEN_EXPIRED * Redirect to clear-cookies * Set SESSION_HAS_BEEN_REFRESHED cookie * Add proper options to refreshed cookie * Check TOKEN_EXPIRED authReason, remove session refreshed cookie
1 parent b1032d6 commit 6c959b0

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { cookies } from 'next/headers'
2+
import { redirect } from 'next/navigation'
3+
4+
export async function GET() {
5+
const cookieStore = cookies()
6+
cookieStore.delete('X-Amzn-Oidc-Data-0')
7+
cookieStore.delete('AWSALBAuthNonce')
8+
redirect('/')
9+
}

frontend/middleware.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const PUBLIC_PATHS = [
1111
'/monitoring',
1212
'/privacy',
1313
'/support',
14+
'/clear-cookies',
1415
]
1516

1617
export async function middleware(req: NextRequest) {
@@ -51,8 +52,10 @@ export async function middleware(req: NextRequest) {
5152
authReason: 'LOCAL_TESTING',
5253
}
5354
}
54-
5555
if (authResult?.isAuthorised !== true) {
56+
if (authResult?.authReason === 'TOKEN_EXPIRED') {
57+
return redirectToClearCookies(req)
58+
}
5659
console.error(`User is not authorised to access ${pathname}`)
5760
return redirectToUnauthorised(req)
5861
}
@@ -79,7 +82,11 @@ function redirectToGenericError(req: NextRequest) {
7982
url.pathname = '/generic-error'
8083
return NextResponse.redirect(url)
8184
}
82-
85+
function redirectToClearCookies(req: NextRequest) {
86+
const url = req.nextUrl.clone()
87+
url.pathname = '/clear-cookies'
88+
return NextResponse.redirect(url)
89+
}
8390
// Configure which paths this middleware should run on
8491
export const config = {
8592
matcher: [

0 commit comments

Comments
 (0)