-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup middleware with csrf protection
- Loading branch information
1 parent
282789c
commit 36613d4
Showing
1 changed file
with
22 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,28 @@ | ||
import { routing } from '@/lib/locale'; | ||
import { verifyRequestOrigin } from 'lucia'; | ||
import createMiddleware from 'next-intl/middleware'; | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
|
||
export default createMiddleware(routing); | ||
const handleI18nRouting = createMiddleware(routing); | ||
|
||
export async function middleware(request: NextRequest): Promise<NextResponse> { | ||
if (request.method !== 'GET') { | ||
const originHeader = request.headers.get('Origin'); | ||
const hostHeader = request.headers.get('Host'); | ||
if ( | ||
!originHeader || | ||
!hostHeader || | ||
!verifyRequestOrigin(originHeader, [hostHeader]) | ||
) { | ||
return new NextResponse(null, { | ||
status: 403, | ||
}); | ||
} | ||
} | ||
|
||
return handleI18nRouting(request); | ||
} | ||
|
||
export const config = { | ||
matcher: ['/', '/(en|no)/:path*', '/((?!api|_next|.*\\..*).*)'], | ||
matcher: ['/', '/(en|no)/:path*'], | ||
}; |