Skip to content

Commit f472a55

Browse files
feat: support for narrowing cors (#12)
* feat: support for narrowing cors * chore: isolate test * chore: test are not isolated
1 parent 91e8357 commit f472a55

5 files changed

Lines changed: 14 additions & 5 deletions

File tree

.env.development.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ GITHUB_CLIENT_SECRET=your_github_oauth_client_secret
33
GITHUB_AUTH_ISSUER=https://your_unique_authentication_issuer
44
JWT_PRIVATE_KEY_PATH=./private-key.pem
55
JWT_PUBLIC_KEY_PATH=./public-key.pem
6-
JWT_KEY_ID=your-api-key-1
6+
JWT_KEY_ID=your-api-key-1
7+
CORS_ORIGIN=

.env.production.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ GITHUB_CLIENT_ID=your_github_oauth_client_id
22
GITHUB_CLIENT_SECRET=your_github_oauth_client_secret
33
GITHUB_AUTH_ISSUER=https://your_unique_authentication_issuer
44
COOKIE_DOMAIN=.yourdomain.com
5-
COOKIE_SAME_SITE=lax
5+
COOKIE_SAME_SITE=lax
6+
CORS_ORIGIN=

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ GITHUB_AUTH_ISSUER=https://your-domain.com/auth/github
3535
> [!NOTE]
3636
> The issuer must be unique for the service. The authentication modules use it to distinguish the providers.
3737
38-
3. (Optional) Configure cookie settings for cross-subdomain support in `.env.production`:
38+
3. (Optional) Configure CORS and cookie settings in `.env.production`:
3939

4040
```bash
41+
CORS_ORIGIN=https://yourapp.yourdomain.com
4142
COOKIE_DOMAIN=.yourdomain.com
4243
COOKIE_SAME_SITE=lax
4344
```

bunfig.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[test]
2-
preload = ["./test-setup.ts"]
2+
preload = ["./test-setup.ts"]

src/server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { exchangePrice, ExchangePriceSchema } from './handlers/exchange/price';
1717

1818
const { version: appVersion, name: appName, description: appDescription } = packageJson;
1919

20+
const corsOrigin = process.env.CORS_ORIGIN;
21+
2022
export const app = new Elysia()
2123
.error({
2224
FetchApiError,
@@ -44,7 +46,11 @@ export const app = new Elysia()
4446
}
4547
})
4648
)
47-
.use(cors())
49+
.use(
50+
cors({
51+
...(corsOrigin !== undefined && { origin: corsOrigin })
52+
})
53+
)
4854
.decorate('github', new GitHubDecorator())
4955
.decorate('jwt', new JwtDecorator())
5056
.decorate('exchange', new ExchangeDecorator())

0 commit comments

Comments
 (0)