Skip to content

Commit ce39802

Browse files
feat: support for cross-subdomain cookie (#4)
1 parent 5b79165 commit ce39802

4 files changed

Lines changed: 134 additions & 211 deletions

File tree

.env.production.example

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

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ GITHUB_AUTH_ISSUER=https://your-domain.com/auth/github
3333
> [!NOTE]
3434
> The issuer must be unique for the service. The authentication modules use it to distinguish the providers.
3535
36-
3. Start the container:
36+
3. (Optional) Configure cookie settings for cross-subdomain support in `.env.production`:
37+
38+
```bash
39+
COOKIE_DOMAIN=.yourdomain.com
40+
COOKIE_SAME_SITE=lax
41+
```
42+
43+
> [!TIP]
44+
> If your API runs on a different subdomain than your frontend (e.g., `api.yourdomain.com` and `app.yourdomain.com`), configure `COOKIE_DOMAIN` with a leading dot (e.g., `.yourdomain.com`) to enable cookie sharing across subdomains. Set `COOKIE_SAME_SITE` to `lax`, `strict`, or `none` as needed. If your API and frontend are on the same domain, you can omit `COOKIE_DOMAIN` or set it without the leading dot.
45+
46+
4. Start the container:
3747

3848
```bash
3949
docker compose up -d

src/handlers/auth/github.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,24 @@ export const githubAuthInit = async ({
3535

3636
const state = await jwt.signOAuthJwt({ payload: stateData });
3737

38+
const production = Bun.env.NODE_ENV === 'production';
39+
const cookieDomain = Bun.env.COOKIE_DOMAIN?.trim();
40+
const cookieSameSite = Bun.env.COOKIE_SAME_SITE?.trim();
41+
3842
auth.set({
3943
value: state,
4044
httpOnly: true,
41-
maxAge: 10 * 60, // 10 minutes, similar as the The GitHub OAuth authorization code which least 10 minutes
42-
path: '/v1/auth/finalize/github'
45+
maxAge: 10 * 60, // 10 minutes, similar as the GitHub OAuth authorization code which least 10 minutes
46+
path: '/v1/auth/finalize/github',
47+
...(production && {
48+
...(cookieDomain !== undefined && cookieDomain !== '' && { domain: cookieDomain }),
49+
...(cookieSameSite !== undefined &&
50+
cookieSameSite !== '' &&
51+
['strict', 'lax', 'none'].includes(cookieSameSite) && {
52+
sameSite: cookieSameSite as 'strict' | 'lax' | 'none'
53+
}),
54+
secure: true
55+
})
4356
});
4457

4558
return { state };

0 commit comments

Comments
 (0)