Skip to content

Commit 794c2a5

Browse files
committed
1 parent 813390e commit 794c2a5

File tree

9 files changed

+106
-49
lines changed

9 files changed

+106
-49
lines changed

frontend/next.config.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
44
const amplifyConfig = require('./amplify_outputs.json');
55

66
const basePath = process.env.NEXT_PUBLIC_BASE_PATH ?? '/templates';
7-
const domain = process.env.NOTIFY_DOMAIN_NAME ?? 'localhost:3000';
87

98
const nextConfig = (phase) => {
109
const isDevServer = phase === PHASE_DEVELOPMENT_SERVER;
@@ -37,42 +36,10 @@ const nextConfig = (phase) => {
3736
basePath: false,
3837
permanent: false,
3938
},
40-
{
41-
source: `${basePath}/auth/inactive`,
42-
destination: '/auth/inactive',
43-
permanent: false,
44-
basePath: false,
45-
},
46-
{
47-
source: `${basePath}/auth/signout`,
48-
destination: '/auth/signout',
49-
basePath: false,
50-
permanent: false,
51-
},
5239
];
5340
},
5441

5542
async rewrites() {
56-
if (includeAuthPages) {
57-
return [
58-
{
59-
source: '/auth/inactive',
60-
destination: `http://${domain}${basePath}/auth/idle`,
61-
basePath: false,
62-
},
63-
{
64-
source: '/auth/signout',
65-
destination: `http://${domain}${basePath}/auth/signout`,
66-
basePath: false,
67-
},
68-
{
69-
source: '/auth',
70-
destination: `http://${domain}${basePath}/auth`,
71-
basePath: false,
72-
},
73-
];
74-
}
75-
7643
return [];
7744
},
7845

frontend/src/__tests__/components/molecules/LogoutWarningModal/__snapshots__/LogoutWarningModal.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ exports[`LogoutWarningModal should match snapshot 1`] = `
4343
<a
4444
class="nhsuk-link"
4545
data-testid="modal-sign-out"
46-
href="/auth/signout"
46+
href="http://localhost:3000/auth/signout"
4747
>
4848
Sign out
4949
</a>

frontend/src/__tests__/components/molecules/__snapshots__/AuthLink.test.tsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`AuthLink renders Sign in link when authStatus changes to unauthenticate
55
<a
66
class="auth-link"
77
data-testid="sign-in-link"
8-
href="/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
8+
href="http://localhost:3000/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
99
id="sign-in-link"
1010
>
1111
Sign in
@@ -18,7 +18,7 @@ exports[`AuthLink renders Sign in link when authStatus is configuring 1`] = `
1818
<a
1919
class="auth-link"
2020
data-testid="sign-in-link"
21-
href="/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
21+
href="http://localhost:3000/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
2222
id="sign-in-link"
2323
>
2424
Sign in
@@ -31,7 +31,7 @@ exports[`AuthLink renders Sign out link when authStatus changes to authenticated
3131
<a
3232
class="auth-link"
3333
data-testid="sign-out-link"
34-
href="/auth/signout"
34+
href="http://localhost:3000/auth/signout"
3535
id="sign-out-link"
3636
>
3737
Sign out

frontend/src/__tests__/components/molecules/__snapshots__/Header.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ exports[`NhsNotifyHeader when authenticated matches snapshot (authenticated) 1`]
8282
<a
8383
class="auth-link nhsuk-header__account-link"
8484
data-testid="sign-out-link"
85-
href="/auth/signout"
85+
href="http://localhost:3000/auth/signout"
8686
id="sign-out-link"
8787
>
8888
Sign out
@@ -176,7 +176,7 @@ exports[`NhsNotifyHeader when unauthenticated matches snapshot (unauthenticated)
176176
<a
177177
class="auth-link nhsuk-header__account-link"
178178
data-testid="sign-in-link"
179-
href="/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
179+
href="http://localhost:3000/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
180180
id="sign-in-link"
181181
>
182182
Sign in
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { getAuthUrl } from '@utils/get-auth-url';
2+
3+
describe('getAuthUrl', () => {
4+
const originalEnv = process.env;
5+
6+
beforeEach(() => {
7+
jest.resetModules();
8+
process.env = { ...originalEnv };
9+
});
10+
11+
afterAll(() => {
12+
process.env = originalEnv;
13+
});
14+
15+
describe('in production', () => {
16+
beforeEach(() => {
17+
Object.defineProperty(process.env, 'NODE_ENV', {
18+
value: 'production',
19+
configurable: true,
20+
});
21+
process.env.NOTIFY_DOMAIN_NAME = 'nhsnotify.nhs.uk';
22+
});
23+
24+
it('should use https protocol', () => {
25+
const result = getAuthUrl('/auth');
26+
expect(result).toBe('https://nhsnotify.nhs.uk/auth');
27+
});
28+
29+
it('should not include basePath for auth app URLs', () => {
30+
const result = getAuthUrl('/auth/signout');
31+
expect(result).toBe('https://nhsnotify.nhs.uk/auth/signout');
32+
});
33+
34+
it('should handle query parameters', () => {
35+
const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate');
36+
expect(result).toBe(
37+
'https://nhsnotify.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate'
38+
);
39+
});
40+
41+
it('should fallback to localhost when NOTIFY_DOMAIN_NAME is not set', () => {
42+
delete process.env.NOTIFY_DOMAIN_NAME;
43+
const result = getAuthUrl('/auth');
44+
expect(result).toBe('https://localhost:3000/auth');
45+
});
46+
});
47+
48+
describe('in development', () => {
49+
beforeEach(() => {
50+
Object.defineProperty(process.env, 'NODE_ENV', {
51+
value: 'development',
52+
configurable: true,
53+
});
54+
});
55+
56+
it('should use http protocol', () => {
57+
const result = getAuthUrl('/auth');
58+
expect(result).toBe('http://localhost:3000/templates/auth');
59+
});
60+
61+
it('should include basePath to hit local auth pages', () => {
62+
process.env.NEXT_PUBLIC_BASE_PATH = '/templates';
63+
const result = getAuthUrl('/auth');
64+
expect(result).toBe('http://localhost:3000/templates/auth');
65+
});
66+
67+
it('should fallback to /templates basePath when NEXT_PUBLIC_BASE_PATH is undefined', () => {
68+
delete process.env.NEXT_PUBLIC_BASE_PATH;
69+
const result = getAuthUrl('/auth');
70+
expect(result).toBe('http://localhost:3000/templates/auth');
71+
});
72+
73+
it('should handle query parameters with basePath', () => {
74+
const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate');
75+
expect(result).toBe(
76+
'http://localhost:3000/templates/auth?redirect=%2Ftemplates%2Fcreate'
77+
);
78+
});
79+
});
80+
});

frontend/src/app/auth/signin/route.dev.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,5 @@ export const GET = async (request: NextRequest) => {
3333
}
3434
}
3535

36-
return NextResponse.json(null, {
37-
status: 307,
38-
headers: {
39-
Location: redirectPath,
40-
},
41-
});
36+
return NextResponse.redirect(redirectPath, { status: 307 });
4237
};

frontend/src/content/content.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ContentBlock } from '@molecules/ContentRenderer/ContentRenderer';
22
import { getBasePath } from '@utils/get-base-path';
3+
import { getAuthUrl } from '@utils/get-auth-url';
34
import { TemplateStatus, TemplateType } from 'nhs-notify-backend-client';
45

56
const generatePageTitle = (title: string): string => {
@@ -25,13 +26,15 @@ const header = {
2526
links: {
2627
signIn: {
2728
text: 'Sign in',
28-
href: `/auth?redirect=${encodeURIComponent(
29-
`${getBasePath()}/create-and-submit-templates`
30-
)}`,
29+
href: getAuthUrl(
30+
`/auth?redirect=${encodeURIComponent(
31+
`${getBasePath()}/create-and-submit-templates`
32+
)}`
33+
),
3134
},
3235
signOut: {
3336
text: 'Sign out',
34-
href: '/auth/signout',
37+
href: getAuthUrl('/auth/signout'),
3538
},
3639
},
3740
},

frontend/src/utils/get-auth-url.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function getAuthUrl(path: string): string {
2+
const protocol = process.env.NODE_ENV === 'production' ? 'https:' : 'http:';
3+
const domain = process.env.NOTIFY_DOMAIN_NAME ?? 'localhost:3000';
4+
5+
const basePath =
6+
process.env.NODE_ENV === 'development'
7+
? (process.env.NEXT_PUBLIC_BASE_PATH ?? '/templates')
8+
: '';
9+
10+
return `${protocol}//${domain}${basePath}${path}`;
11+
}

infrastructure/terraform/components/app/amplify_app.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ resource "aws_amplify_app" "main" {
3434
CSRF_SECRET = aws_ssm_parameter.csrf_secret.value
3535
NEXT_PUBLIC_PROMPT_SECONDS_BEFORE_LOGOUT = 120
3636
NEXT_PUBLIC_TIME_TILL_LOGOUT_SECONDS = 900
37+
NOTIFY_DOMAIN_NAME = local.root_domain_name
3738
NOTIFY_ENVIRONMENT = var.environment
3839
NOTIFY_GROUP = var.group
3940
USER_POOL_CLIENT_ID = jsondecode(aws_ssm_parameter.cognito_config.value)["USER_POOL_CLIENT_ID"]

0 commit comments

Comments
 (0)