Skip to content

Commit 5362455

Browse files
committed
CCM-7465: re-add protected routes
1 parent 5b57282 commit 5362455

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

frontend/src/middleware.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { cookies } from 'next/headers';
21
import { NextResponse, type NextRequest } from 'next/server';
3-
// import { getAccessTokenServer } from '@utils/amplify-utils';
2+
import { getAccessTokenServer } from '@utils/amplify-utils';
3+
import { getBasePath } from '@utils/get-base-path';
44

55
function isExcludedPath(path: string, excludedPaths: string[]): boolean {
66
return excludedPaths.some((excludedPath) => path.startsWith(excludedPath));
@@ -9,26 +9,23 @@ function isExcludedPath(path: string, excludedPaths: string[]): boolean {
99
export async function middleware(request: NextRequest) {
1010
const excludedPaths = ['/create-and-submit-templates', '/auth'];
1111

12-
console.log('middleware', cookies().getAll());
13-
1412
if (isExcludedPath(request.nextUrl.pathname, excludedPaths)) {
1513
return NextResponse.next();
1614
}
1715

18-
// const token = await getAccessTokenServer();
16+
const token = await getAccessTokenServer();
1917

20-
// console.log('token', token);
21-
// if (!token) {
22-
// return Response.redirect(
23-
// new URL(
24-
// `/auth?redirect=${encodeURIComponent(
25-
// // Replace create-and-submit-templates with ${request.nextUrl.pathname} once auth login is fixed otherwise we end up in a redirect loop
26-
// `${getBasePath()}/create-and-submit-templates`
27-
// )}`,
28-
// request.url
29-
// )
30-
// );
31-
// }
18+
if (!token) {
19+
return Response.redirect(
20+
new URL(
21+
`/auth?redirect=${encodeURIComponent(
22+
// Replace create-and-submit-templates with ${request.nextUrl.pathname} once auth login is fixed otherwise we end up in a redirect loop
23+
`${getBasePath()}/create-and-submit-templates`
24+
)}`,
25+
request.url
26+
)
27+
);
28+
}
3229
}
3330

3431
export const config = {

frontend/src/utils/amplify-utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { generateServerClientUsingCookies } from '@aws-amplify/adapter-nextjs/da
77
import { Schema } from 'nhs-notify-web-template-management-amplify';
88
import { createServerRunner } from '@aws-amplify/adapter-nextjs';
99
import { fetchAuthSession } from 'aws-amplify/auth/server';
10-
import { logger } from 'nhs-notify-web-template-management-utils/logger';
1110

1211
const config = require('@/amplify_outputs.json');
1312

@@ -23,19 +22,14 @@ export const getAmplifyBackendClient = () =>
2322
});
2423

2524
export async function getAccessTokenServer(): Promise<string | undefined> {
26-
logger.info('cookies', await cookies().getAll());
27-
logger.info('cookies 2', await cookies().toString());
2825
try {
2926
const { tokens } = await runWithAmplifyServerContext({
3027
nextServerContext: { cookies },
3128
operation: fetchAuthSession,
3229
});
3330

34-
console.log('tokens', tokens);
35-
3631
return tokens?.accessToken?.toString();
37-
} catch (error) {
32+
} catch {
3833
// no-op
39-
logger.error('issue with auth', error);
4034
}
4135
}

tests/test-team/template-mgmt-component-tests/template-mgmt-start-page.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { test, expect } from '@playwright/test';
22
import { TemplateMgmtStartPage } from '../pages/template-mgmt-start-page';
3-
import { assertLoginLink } from './template-mgmt-common.steps';
4-
5-
// Reset storage state for this file to avoid being authenticated
6-
test.use({ storageState: { cookies: [], origins: [] } });
3+
import { assertLogoutLink } from './template-mgmt-common.steps';
74

85
test.describe('Start Page', () => {
96
test('should land on start page when navigating to "/templates/create-and-submit-templates"', async ({
@@ -49,10 +46,10 @@ test.describe('Start Page', () => {
4946
);
5047
});
5148

52-
test('should display login link', async ({ page }) => {
49+
test('should display logout link', async ({ page }) => {
5350
const startPage = new TemplateMgmtStartPage(page);
5451

55-
await assertLoginLink({
52+
await assertLogoutLink({
5653
page: startPage,
5754
id: '/templates/create-and-submit-templates',
5855
});

0 commit comments

Comments
 (0)