@@ -2,6 +2,39 @@ import { NextResponse, type NextRequest } from 'next/server';
22import { getAccessTokenServer } from '@utils/amplify-utils' ;
33import { getBasePath } from '@utils/get-base-path' ;
44
5+ const protectedPaths = [
6+ / ^ \/ c h o o s e - a - t e m p l a t e - t y p e $ / ,
7+ / ^ \/ c o p y - t e m p l a t e \/ [ ^ / ] + $ / ,
8+ / ^ \/ c r e a t e - e m a i l - t e m p l a t e $ / ,
9+ / ^ \/ c r e a t e - n h s - a p p - t e m p l a t e $ / ,
10+ / ^ \/ c r e a t e - t e x t - m e s s a g e - t e m p l a t e $ / ,
11+ / ^ \/ d e l e t e - t e m p l a t e \/ [ ^ / ] + $ / ,
12+ / ^ \/ e d i t - e m a i l - t e m p l a t e \/ [ ^ / ] + $ / ,
13+ / ^ \/ e d i t - n h s - a p p - t e m p l a t e / ,
14+ / ^ \/ e d i t - t e x t - m e s s a g e - t e m p l a t e \/ [ ^ / ] + $ / ,
15+ / ^ \/ e m a i l - t e m p l a t e - s u b m i t t e d \/ [ ^ / ] + $ / ,
16+ / ^ \/ i n v a l i d - t e m p l a t e $ / ,
17+ / ^ \/ m a n a g e - t e m p l a t e s $ / ,
18+ / ^ \/ n h s - a p p - t e m p l a t e - s u b m i t t e d \/ [ ^ / ] + $ / ,
19+ / ^ \/ p r e v i e w - e m a i l - t e m p l a t e \/ [ ^ / ] + $ / ,
20+ / ^ \/ p r e v i e w - n h s - a p p - t e m p l a t e \/ [ ^ / ] + $ / ,
21+ / ^ \/ p r e v i e w - t e x t - m e s s a g e - t e m p l a t e \/ [ ^ / ] + $ / ,
22+ / ^ \/ s u b m i t - e m a i l - t e m p l a t e \/ [ ^ / ] + $ / ,
23+ / ^ \/ s u b m i t - n h s - a p p - t e m p l a t e \/ [ ^ / ] + $ / ,
24+ / ^ \/ s u b m i t - t e x t - m e s s a g e - t e m p l a t e \/ [ ^ / ] + $ / ,
25+ / ^ \/ t e x t - m e s s a g e - t e m p l a t e - s u b m i t t e d \/ [ ^ / ] + $ / ,
26+ / ^ \/ v i e w - s u b m i t t e d - e m a i l - t e m p l a t e \/ [ ^ / ] + $ / ,
27+ / ^ \/ v i e w - s u b m i t t e d - n h s - a p p - t e m p l a t e \/ [ ^ / ] + $ / ,
28+ / ^ \/ v i e w - s u b m i t t e d - t e x t - m e s s a g e - t e m p l a t e \/ [ ^ / ] + $ / ,
29+ ] ;
30+
31+ const publicPaths = [
32+ / ^ \/ c r e a t e - a n d - s u b m i t - t e m p l a t e s $ / ,
33+ / ^ \/ a u t h $ / ,
34+ / ^ \/ a u t h \/ s i g n i n $ / ,
35+ / ^ \/ a u t h \/ s i g n o u t $ / ,
36+ ] ;
37+
538function getContentSecurityPolicy ( nonce : string ) {
639 const contentSecurityPolicyDirective = {
740 'base-uri' : [ `'self'` ] ,
@@ -28,21 +61,17 @@ function getContentSecurityPolicy(nonce: string) {
2861 . join ( '; ' ) ;
2962}
3063
31- function isPublicPath ( path : string , publicPaths : string [ ] ) : boolean {
32- return publicPaths . some ( ( publicPath ) => path . startsWith ( publicPath ) ) ;
33- }
34-
3564export async function middleware ( request : NextRequest ) {
65+ const { pathname } = request . nextUrl ;
66+
3667 const nonce = Buffer . from ( crypto . randomUUID ( ) ) . toString ( 'base64' ) ;
3768
3869 const csp = getContentSecurityPolicy ( nonce ) ;
3970
4071 const requestHeaders = new Headers ( request . headers ) ;
4172 requestHeaders . set ( 'Content-Security-Policy' , csp ) ;
4273
43- const publicPaths = [ '/create-and-submit-templates' , '/auth' , '/lib' ] ;
44-
45- if ( isPublicPath ( request . nextUrl . pathname , publicPaths ) ) {
74+ if ( publicPaths . some ( ( p ) => p . test ( pathname ) ) ) {
4675 const publicPathResponse = NextResponse . next ( {
4776 request : {
4877 headers : requestHeaders ,
@@ -54,6 +83,10 @@ export async function middleware(request: NextRequest) {
5483 return publicPathResponse ;
5584 }
5685
86+ if ( ! protectedPaths . some ( ( p ) => p . test ( pathname ) ) ) {
87+ return new NextResponse ( 'Page not found' , { status : 404 } ) ;
88+ }
89+
5790 const token = await getAccessTokenServer ( ) ;
5891
5992 if ( ! token ) {
0 commit comments