Skip to content

Commit f3fc581

Browse files
matteioophoenix-ru
andauthored
feat: Add support for secure attribute of local/refresh provider cookies (#729)
* Fixed misplaced comment and added comment for the duration in human-readable time. * Added secure cookie attribute for local and refresh provider. * Set secure attribute of token to false by default. * Added documentation for the added secureCookieAttribute. * Update useAuthState.ts --------- Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com>
1 parent a863d92 commit f3fc581

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

docs/content/2.configuration/2.nuxt-config.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,20 @@ type ProviderLocal = {
240240
*/
241241
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined,
242242
/**
243-
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
243+
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
244+
*
245+
* @default false
246+
* @example true
247+
*/
248+
secureCookieAttribute?: boolean,
249+
/**
250+
* The cookie domain.
251+
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
244252
*
245253
* @default ''
246-
* @example sidebase.io
254+
* @example 'sidebase.io'
247255
*/
248-
cookieDomain?: string;
256+
cookieDomain?: string,
249257
},
250258
/*
251259
* Settings for the session-data that `nuxt-auth` receives from the `getSession` endpoint.
@@ -401,12 +409,20 @@ type ProviderRefresh = {
401409
*/
402410
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined,
403411
/**
404-
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
412+
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
413+
*
414+
* @default false
415+
* @example true
416+
*/
417+
secureCookieAttribute?: boolean,
418+
/**
419+
* The cookie domain.
420+
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
405421
*
406422
* @default ''
407-
* @example sidebase.io
423+
* @example 'sidebase.io'
408424
*/
409-
cookieDomain?: string;
425+
cookieDomain?: string,
410426
},
411427
/**
412428
* Settings for the authentication-refreshToken that `nuxt-auth` receives from the `signIn` endpoint and that can be used to authenticate subsequent requests.

src/module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ const defaultsByBackend: {
5656
type: 'Bearer',
5757
cookieName: 'auth.token',
5858
headerName: 'Authorization',
59-
maxAgeInSeconds: 30 * 60,
59+
maxAgeInSeconds: 30 * 60, // 30 minutes
6060
sameSiteAttribute: 'lax',
61+
secureCookieAttribute: false,
6162
cookieDomain: ''
6263
},
6364
session: {
@@ -86,13 +87,15 @@ const defaultsByBackend: {
8687
headerName: 'Authorization',
8788
maxAgeInSeconds: 5 * 60, // 5 minutes
8889
sameSiteAttribute: 'none',
90+
secureCookieAttribute: false,
8991
cookieDomain: ''
9092
},
9193
refreshToken: {
9294
signInResponseRefreshTokenPointer: '/refreshToken',
9395
refreshRequestTokenPointer: '/refreshToken',
9496
cookieName: 'auth.refresh-token',
9597
maxAgeInSeconds: 60 * 60 * 24 * 7, // 7 days
98+
secureCookieAttribute: false,
9699
cookieDomain: ''
97100
},
98101
session: {

src/runtime/composables/local/useAuthState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export const useAuthState = (): UseAuthStateReturn => {
2929
default: () => null,
3030
domain: config.token.cookieDomain,
3131
maxAge: config.token.maxAgeInSeconds,
32-
sameSite: config.token.sameSiteAttribute
32+
sameSite: config.token.sameSiteAttribute,
33+
secure: config.token.secureCookieAttribute
3334
})
3435

3536
const rawToken = useState('auth:raw-token', () => _rawTokenCookie.value)

src/runtime/composables/refresh/useAuthState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const useAuthState = (): UseAuthStateReturn => {
1919
default: () => null,
2020
domain: config.refreshToken.cookieDomain,
2121
maxAge: config.refreshToken.maxAgeInSeconds,
22-
sameSite: 'lax'
22+
sameSite: 'lax',
23+
secure: config.refreshToken.secureCookieAttribute
2324
}
2425
)
2526

src/runtime/types.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,18 @@ export type ProviderLocal = {
168168
*/
169169
sameSiteAttribute?: boolean | 'lax' | 'strict' | 'none' | undefined;
170170
/**
171-
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
171+
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
172+
*
173+
* @default false
174+
* @example true
175+
*/
176+
secureCookieAttribute?: boolean;
177+
/**
178+
* The cookie domain.
179+
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
172180
*
173181
* @default ''
174-
* @example sidebase.io
182+
* @example 'sidebase.io'
175183
*/
176184
cookieDomain?: string;
177185
};
@@ -270,10 +278,18 @@ export type ProviderLocalRefresh = Omit<ProviderLocal, 'type'> & {
270278
*/
271279
maxAgeInSeconds?: number;
272280
/**
273-
* The cookie domain. See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
281+
* Whether to set the secure flag on the cookie. This is useful when the application is served over HTTPS.
282+
*
283+
* @default false
284+
* @example true
285+
*/
286+
secureCookieAttribute?: boolean;
287+
/**
288+
* The cookie domain.
289+
* See the specification here: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.3
274290
*
275291
* @default ''
276-
* @example sidebase.io
292+
* @example 'sidebase.io'
277293
*/
278294
cookieDomain?: string;
279295
};

0 commit comments

Comments
 (0)