Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 7abc24d

Browse files
feat: add configurable auth-cookie domain property (#22)
* Add configurable cookie domain property * fix: parametr description * fix: property definition expanded * Update src/module.ts Co-authored-by: Nils <nils.jonalik@rwth-aachen.de> Co-authored-by: Nils <nils.jonalik@rwth-aachen.de>
1 parent 5cbbad4 commit 7abc24d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ declare interface SessionOptions {
4444
* @docs https://github.com/unjs/unstorage
4545
*/
4646
storageOptions: CreateStorageOptions,
47+
/**
48+
* Set the domain the session cookie will be receivable by. Setting `domain: null` results in setting the domain the cookie is initially set on. Specifying a domain will allow the domain and all its sub-domains.
49+
* @default null
50+
* @example '.example.com'
51+
* @type string | null
52+
* @docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent
53+
*/
54+
domain: string | null
4755
}
4856

4957
declare interface ApiOptions {
@@ -100,6 +108,7 @@ const defaults: ModuleOptions = {
100108
idLength: 64,
101109
storePrefix: 'sessions',
102110
cookieSameSite: 'lax',
111+
domain: null,
103112
storageOptions: {}
104113
},
105114
api: {

src/runtime/server/middleware/session/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const safeSetCookie = (event: H3Event, name: string, value: string) => setCookie
1414
// Only send cookie via HTTP requests, do not allow access of cookie from JS to mitigate XSS attacks
1515
httpOnly: true,
1616
// Do not send cookies on many cross-site requests to mitigates CSRF and cross-site attacks, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#lax
17-
sameSite: useRuntimeConfig().session.session.cookieSameSite as SameSiteOptions
17+
sameSite: useRuntimeConfig().session.session.cookieSameSite as SameSiteOptions,
18+
// Set cookie for subdomain
19+
domain: useRuntimeConfig().session.session.domain,
1820
})
1921

2022
export declare interface Session {

0 commit comments

Comments
 (0)