Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
const amplifyConfig = require('./amplify_outputs.json');

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

const nextConfig = (phase) => {
const isDevServer = phase === PHASE_DEVELOPMENT_SERVER;
Expand Down Expand Up @@ -37,42 +36,10 @@ const nextConfig = (phase) => {
basePath: false,
permanent: false,
},
{
source: `${basePath}/auth/inactive`,
destination: '/auth/inactive',
permanent: false,
basePath: false,
},
{
source: `${basePath}/auth/signout`,
destination: '/auth/signout',
basePath: false,
permanent: false,
},
];
},

async rewrites() {
if (includeAuthPages) {
return [
{
source: '/auth/inactive',
destination: `http://${domain}${basePath}/auth/idle`,
basePath: false,
},
{
source: '/auth/signout',
destination: `http://${domain}${basePath}/auth/signout`,
basePath: false,
},
{
source: '/auth',
destination: `http://${domain}${basePath}/auth`,
basePath: false,
},
];
}

return [];
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ exports[`LogoutWarningModal should match snapshot 1`] = `
<a
class="nhsuk-link"
data-testid="modal-sign-out"
href="/auth/signout"
href="http://localhost:3000/auth/signout"
>
Sign out
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`AuthLink renders Sign in link when authStatus changes to unauthenticate
<a
class="auth-link"
data-testid="sign-in-link"
href="/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
href="http://localhost:3000/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
id="sign-in-link"
>
Sign in
Expand All @@ -18,7 +18,7 @@ exports[`AuthLink renders Sign in link when authStatus is configuring 1`] = `
<a
class="auth-link"
data-testid="sign-in-link"
href="/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
href="http://localhost:3000/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
id="sign-in-link"
>
Sign in
Expand All @@ -31,7 +31,7 @@ exports[`AuthLink renders Sign out link when authStatus changes to authenticated
<a
class="auth-link"
data-testid="sign-out-link"
href="/auth/signout"
href="http://localhost:3000/auth/signout"
id="sign-out-link"
>
Sign out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ exports[`NhsNotifyHeader when authenticated matches snapshot (authenticated) 1`]
<a
class="auth-link nhsuk-header__account-link"
data-testid="sign-out-link"
href="/auth/signout"
href="http://localhost:3000/auth/signout"
id="sign-out-link"
>
Sign out
Expand Down Expand Up @@ -176,7 +176,7 @@ exports[`NhsNotifyHeader when unauthenticated matches snapshot (unauthenticated)
<a
class="auth-link nhsuk-header__account-link"
data-testid="sign-in-link"
href="/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
href="http://localhost:3000/auth?redirect=%2Ftemplates%2Fcreate-and-submit-templates"
id="sign-in-link"
>
Sign in
Expand Down
189 changes: 189 additions & 0 deletions frontend/src/__tests__/utils/get-auth-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { getAuthUrl } from '@utils/get-auth-url';

describe('getAuthUrl', () => {
const originalWindow = { ...global.window };
const originalEnv = { ...process.env };

afterAll(() => {
Object.defineProperty(process, 'env', {
value: originalEnv,
configurable: true,
});

Object.defineProperty(global, 'window', {
value: originalWindow,
configurable: true,
});
});

describe('client side (when window is available)', () => {
beforeEach(() => {
Object.defineProperty(global, 'window', {
value: {
location: {
protocol: 'https:',
host: 'nhsnotify.national.nhs.uk',
},
},
configurable: true,
});
});

afterAll(() => {
Object.defineProperty(global, 'window', {
value: originalWindow,
configurable: true,
});
});

it('should construct URL', () => {
const result = getAuthUrl('/auth');
expect(result).toBe('https://nhsnotify.national.nhs.uk/auth');
});

it('should handle query parameters', () => {
const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate');

expect(result).toBe(
'https://nhsnotify.national.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate'
);
});

describe('in development env', () => {
beforeEach(() => {
Object.defineProperty(process.env, 'NODE_ENV', {
value: 'development',
configurable: true,
});
});

afterAll(() => {
Object.defineProperty(process.env, 'NODE_ENV', {
value: originalEnv,
configurable: true,
});
});

it('should include the base path set', () => {
process.env.NEXT_PUBLIC_BASE_PATH = '/base-path';

const result = getAuthUrl('/auth');
expect(result).toBe('https://nhsnotify.national.nhs.uk/base-path/auth');
});

it('should fallback to templates when no base path environment variable provided', () => {
delete process.env.NEXT_PUBLIC_BASE_PATH;

const result = getAuthUrl('/auth');
expect(result).toBe('https://nhsnotify.national.nhs.uk/templates/auth');
});
});
});

describe('when window is not available', () => {
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (global as any).window;
});

afterAll(() => {
global.window = originalWindow;
});

describe('when gateway URL environment variable is available', () => {
beforeEach(() => {
process.env.NEXT_PUBLIC_GATEWAY_URL =
'https://dev.web-gateway.nhsnotify.national.nhs.uk';
});

it('should use NEXT_PUBLIC_GATEWAY_URL', () => {
const result = getAuthUrl('/auth');
expect(result).toBe(
'https://dev.web-gateway.nhsnotify.national.nhs.uk/auth'
);
});

it('should handle query parameters', () => {
const result = getAuthUrl('/auth?redirect=%2Ftemplates%2Fcreate');
expect(result).toBe(
'https://dev.web-gateway.nhsnotify.national.nhs.uk/auth?redirect=%2Ftemplates%2Fcreate'
);
});

describe('in development env', () => {
beforeEach(() => {
Object.defineProperty(process.env, 'NODE_ENV', {
value: 'development',
configurable: true,
});
});

afterAll(() => {
Object.defineProperty(process.env, 'NODE_ENV', {
value: originalEnv,
configurable: true,
});
});

it('should include the base path set', () => {
process.env.NEXT_PUBLIC_BASE_PATH = '/base-path';

const result = getAuthUrl('/auth');
expect(result).toBe(
'https://dev.web-gateway.nhsnotify.national.nhs.uk/base-path/auth'
);
});

it('should fallback to templates when no base path environment variable provided', () => {
delete process.env.NEXT_PUBLIC_BASE_PATH;

const result = getAuthUrl('/auth');
expect(result).toBe(
'https://dev.web-gateway.nhsnotify.national.nhs.uk/templates/auth'
);
});
});
});

describe('when no gateway URL environment variable is available', () => {
beforeEach(() => {
delete process.env.NEXT_PUBLIC_GATEWAY_URL;
});

it('should fallback to localhost:3000', () => {
const result = getAuthUrl('/auth');
expect(result).toBe('http://localhost:3000/auth');
});

describe('in development env', () => {
beforeEach(() => {
Object.defineProperty(process.env, 'NODE_ENV', {
value: 'development',
configurable: true,
});
});

afterAll(() => {
Object.defineProperty(process.env, 'NODE_ENV', {
value: originalEnv,
configurable: true,
});
});

it('should include the base path set', () => {
process.env.NEXT_PUBLIC_BASE_PATH = '/base-path';

const result = getAuthUrl('/auth');
expect(result).toBe('http://localhost:3000/base-path/auth');
});

it('should fallback to templates when no base path environment variable provided', () => {
delete process.env.NEXT_PUBLIC_BASE_PATH;

const result = getAuthUrl('/auth');
expect(result).toBe('http://localhost:3000/templates/auth');
});
});
});
});
});
7 changes: 1 addition & 6 deletions frontend/src/app/auth/signin/route.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,5 @@ export const GET = async (request: NextRequest) => {
}
}

return NextResponse.json(null, {
status: 307,
headers: {
Location: redirectPath,
},
});
return NextResponse.redirect(redirectPath, { status: 307 });
};
11 changes: 7 additions & 4 deletions frontend/src/content/content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ContentBlock } from '@molecules/ContentRenderer/ContentRenderer';
import { getBasePath } from '@utils/get-base-path';
import { getAuthUrl } from '@utils/get-auth-url';
import { TemplateStatus, TemplateType } from 'nhs-notify-backend-client';

const generatePageTitle = (title: string): string => {
Expand All @@ -25,13 +26,15 @@ const header = {
links: {
signIn: {
text: 'Sign in',
href: `/auth?redirect=${encodeURIComponent(
`${getBasePath()}/create-and-submit-templates`
)}`,
href: getAuthUrl(
`/auth?redirect=${encodeURIComponent(
`${getBasePath()}/create-and-submit-templates`
)}`
),
},
signOut: {
text: 'Sign out',
href: '/auth/signout',
href: getAuthUrl('/auth/signout'),
},
},
},
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/utils/get-auth-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function getBasePath(): string {
return process.env.NODE_ENV === 'development'
? (process.env.NEXT_PUBLIC_BASE_PATH ?? '/templates')
: '';
}

export function getAuthUrl(path: string): string {
const basePath = getBasePath();

if (typeof window !== 'undefined') {
return `${window.location.protocol}//${window.location.host}${basePath}${path}`;
}

const gatewayUrl = process.env.NEXT_PUBLIC_GATEWAY_URL;
if (gatewayUrl) {
return `${gatewayUrl}${basePath}${path}`;
}

return `http://localhost:3000${basePath}${path}`;
}
2 changes: 2 additions & 0 deletions infrastructure/terraform/components/app/amplify_app.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ resource "aws_amplify_app" "main" {
AMPLIFY_MONOREPO_APP_ROOT = "frontend"
API_BASE_URL = module.backend_api.api_base_url
CSRF_SECRET = aws_ssm_parameter.csrf_secret.value
NEXT_PUBLIC_GATEWAY_URL = local.gateway_url
NEXT_PUBLIC_PROMPT_SECONDS_BEFORE_LOGOUT = 120
NEXT_PUBLIC_TIME_TILL_LOGOUT_SECONDS = 900
NOTIFY_DOMAIN_NAME = local.root_domain_name
NOTIFY_ENVIRONMENT = var.environment
NOTIFY_GROUP = var.group
USER_POOL_CLIENT_ID = jsondecode(aws_ssm_parameter.cognito_config.value)["USER_POOL_CLIENT_ID"]
Expand Down
5 changes: 5 additions & 0 deletions infrastructure/terraform/components/app/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ locals {
root_domain_name = "${var.environment}.${local.acct.dns_zone["name"]}"
lambdas_source_code_dir = "../../../../lambdas"
log_destination_arn = "arn:aws:logs:${var.region}:${var.observability_account_id}:destination:nhs-main-obs-firehose-logs"
gateway_url = var.gateway_domain != null ? (
var.use_environment_specific_gateway_domain
? "https://${var.environment}.${var.gateway_domain}"
: "https://${var.gateway_domain}"
) : "https://${aws_amplify_app.main.default_domain}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module "amplify_branch" {
enable_auto_build = false

environment_variables = {
NOTIFY_SUBDOMAIN = var.environment
NEXT_PUBLIC_BASE_PATH = "/templates"
NOTIFY_SUBDOMAIN = var.environment
NEXT_PUBLIC_BASE_PATH = "/templates"
NEXT_PUBLIC_GATEWAY_URL = local.gateway_url
}
}
1 change: 1 addition & 0 deletions infrastructure/terraform/components/app/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ output "amplify" {
id = aws_amplify_app.main.id
domain_name = local.root_domain_name
branch_name = var.branch_name
gateway_url = local.gateway_url
}
}

Expand Down
Loading
Loading