-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server, cors): simplified cors configuration (#67)
- Loading branch information
1 parent
da85a10
commit 57330fc
Showing
5 changed files
with
15 additions
and
19 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
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
14 changes: 5 additions & 9 deletions
14
packages/app-server/src/modules/app/middlewares/cors.middleware.ts
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,15 +1,11 @@ | ||
import { cors } from 'hono/cors'; | ||
import { createMiddleware } from 'hono/factory'; | ||
import type { Context } from '../server.types'; | ||
|
||
export const corsMiddleware = cors({ | ||
origin: (origin, context: Context) => { | ||
const allowedOrigins = context.get('config').server.corsOrigin; | ||
export const corsMiddleware = createMiddleware(async (context: Context, next) => { | ||
const { server: { corsOrigins } } = context.get('config'); | ||
|
||
if (allowedOrigins.length === 1 && allowedOrigins[0] === '*') { | ||
return origin; | ||
} | ||
const corsHandler = cors({ origin: corsOrigins }); | ||
|
||
return allowedOrigins.find(allowedOrigin => allowedOrigin === origin); | ||
}, | ||
credentials: true, | ||
return corsHandler(context, next); | ||
}); |
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