-
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.
- Loading branch information
1 parent
7f771e7
commit a5410de
Showing
7 changed files
with
39 additions
and
41 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,12 +1,33 @@ | ||
// middleware.ts | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { alternateBrandUrl, brandUrl, brandUrlShort } from 'types/constants'; | ||
|
||
// This function can be marked `async` if using `await` inside | ||
export async function middleware(request: NextRequest) { | ||
return NextResponse.next(); | ||
// the list of all allowed origins | ||
const allowedOrigins = [brandUrl, brandUrlShort, ...[alternateBrandUrl]]; | ||
// retrieve the current response | ||
const res = NextResponse.next(); | ||
// retrieve the HTTP "Origin" header | ||
// from the incoming request | ||
request.headers.get('origin'); | ||
// if the origin is an allowed one, | ||
// add it to the 'Access-Control-Allow-Origin' header | ||
if (allowedOrigins.includes(origin)) { | ||
res.headers.append('Access-Control-Allow-Origin', origin); | ||
} | ||
// add the remaining CORS headers to the response | ||
res.headers.append('Access-Control-Allow-Credentials', 'true'); | ||
res.headers.append('Access-Control-Allow-Methods', 'GET,DELETE,PATCH,POST,PUT'); | ||
res.headers.append( | ||
'Access-Control-Allow-Headers', | ||
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version', | ||
); | ||
|
||
return res; | ||
} | ||
|
||
// Limit the middleware to paths starting with `/api/` | ||
export const config = { | ||
matcher: '/api/:function*', | ||
matcher: '/api/:path*', | ||
}; |
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
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
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,4 +1,3 @@ | ||
import { allowCors } from 'api/axios'; | ||
import { f } from 'controllers'; | ||
// f = forward | ||
export default allowCors(f.handler); | ||
export default f.handler; |
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,4 +1,3 @@ | ||
import { allowCors } from 'api/axios'; | ||
import { forward } from 'controllers'; | ||
|
||
export default allowCors(forward.handler); | ||
export default forward.handler; |
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,4 +1,3 @@ | ||
import { allowCors } from 'api/axios'; | ||
import { i } from 'controllers'; | ||
// i = mage | ||
export default allowCors(i.handler); | ||
export default i.handler; |
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