-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added middleware to handle validations
- Loading branch information
Showing
2 changed files
with
39 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
import { validateEnv } from "@/env.mjs"; | ||
|
||
export function middleware(request: NextRequest) { | ||
try { | ||
// Validate client-side environment variables | ||
validateEnv(); | ||
} catch (error: any) { | ||
console.error(error.message); | ||
|
||
// Optionally, you can customize the response or redirect to an error page | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: 'Internal Server Error', | ||
message: error.message, | ||
}), | ||
{ | ||
status: 500, | ||
headers: { 'Content-Type': 'application/json' }, | ||
} | ||
); | ||
} | ||
|
||
// Proceed with the request if validation passes | ||
return NextResponse.next(); | ||
} | ||
|
||
// Configure the middleware to match all routes except for static files and API routes | ||
export const config = { | ||
matcher: [ | ||
/* | ||
* Match all request paths except for the ones starting with: | ||
* - /_next/image (image optimization files) | ||
* - /favicon.ico (favicon file) | ||
*/ | ||
'/((?!api|_next/static|_next/image|favicon.ico).*)', | ||
], | ||
}; |
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