Skip to content

Commit

Permalink
feat: Add cookie domain config (#736)
Browse files Browse the repository at this point in the history
* Cookie domain config

* clean up

* default empty string
  • Loading branch information
pier-lucRVezy authored Apr 25, 2024
1 parent 6ad830b commit 9bd9f45
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
21 changes: 21 additions & 0 deletions docs/content/2.configuration/2.nuxt-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ type ProviderLocal = {
* @example 'strict'
*/
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined,
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
*/
cookieDomain?: string;
},
/**
* Define an interface for the session data object that `nuxt-auth` expects to receive from the `getSession` endpoint.
Expand Down Expand Up @@ -376,6 +383,13 @@ type ProviderRefresh = {
* @example 'strict'
*/
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined,
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
*/
cookieDomain?: string;
},
/**
* Settings for the authentication-refreshToken that `nuxt-auth` receives from the `signIn` endpoint and that can be used to authenticate subsequent requests.
Expand Down Expand Up @@ -423,6 +437,13 @@ type ProviderRefresh = {
* @example 60 * 60 * 24
*/
maxAgeInSeconds?: number,
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
*/
cookieDomain?: string;
},
/**
* Define an interface for the session data object that `nuxt-auth` expects to receive from the `getSession` endpoint.
Expand Down
11 changes: 7 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const defaultsByBackend: {
cookieName: 'auth.token',
headerName: 'Authorization',
maxAgeInSeconds: 30 * 60,
sameSiteAttribute: 'lax'
sameSiteAttribute: 'lax',
cookieDomain: ''
},
sessionDataType: { id: 'string | number' }
},
Expand All @@ -79,14 +80,16 @@ const defaultsByBackend: {
type: 'Bearer',
cookieName: 'auth.token',
headerName: 'Authorization',
maxAgeInSeconds: 5 * 60,
sameSiteAttribute: 'none' // 5 minutes
maxAgeInSeconds: 5 * 60, // 5 minutes
sameSiteAttribute: 'none',
cookieDomain: ''
},
refreshToken: {
signInResponseRefreshTokenPointer: '/refreshToken',
refreshRequestTokenPointer: '/refreshToken',
cookieName: 'auth.refresh-token',
maxAgeInSeconds: 60 * 60 * 24 * 7 // 7 days
maxAgeInSeconds: 60 * 60 * 24 * 7, // 7 days
cookieDomain: ''
},
sessionDataType: { id: 'string | number' }
},
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/composables/local/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export const useAuthState = (): UseAuthStateReturn => {
const commonAuthState = makeCommonAuthState<SessionData>()

// Re-construct state from cookie, also setup a cross-component sync via a useState hack, see https://github.com/nuxt/nuxt/issues/13020#issuecomment-1397282717
const _rawTokenCookie = useCookie<string | null>(config.token.cookieName, { default: () => null, maxAge: config.token.maxAgeInSeconds, sameSite: config.token.sameSiteAttribute })
const _rawTokenCookie = useCookie<string | null>(config.token.cookieName, {
default: () => null,
domain: config.token.cookieDomain,
maxAge: config.token.maxAgeInSeconds,
sameSite: config.token.sameSiteAttribute
})

const rawToken = useState('auth:raw-token', () => _rawTokenCookie.value)
watch(rawToken, () => { _rawTokenCookie.value = rawToken.value })
Expand Down
1 change: 1 addition & 0 deletions src/runtime/composables/refresh/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const useAuthState = (): UseAuthStateReturn => {
config.refreshToken.cookieName,
{
default: () => null,
domain: config.refreshToken.cookieDomain,
maxAge: config.refreshToken.maxAgeInSeconds,
sameSite: 'lax'
}
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ export type ProviderLocal = {
* @example 'strict'
*/
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined;
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
*/
cookieDomain?: string;
};
/**
* Define an interface for the session data object that `nuxt-auth` expects to receive from the `getSession` endpoint.
Expand Down Expand Up @@ -245,6 +252,13 @@ export type ProviderLocalRefresh = Omit<ProviderLocal, 'type'> & {
* Note: Your backend may reject / expire the token earlier / differently.
*/
maxAgeInSeconds?: number;
/**
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
*
* @default ''
* @example sidebase.io
*/
cookieDomain?: string;
};
};

Expand Down

0 comments on commit 9bd9f45

Please sign in to comment.