Skip to content

Commit

Permalink
feat(session): ✨ Added expirationThreshold setting
Browse files Browse the repository at this point in the history
  • Loading branch information
itpropro committed Jan 17, 2024
1 parent 171b63c commit 9933b8e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default defineNuxtConfig({
session: {
expirationCheck: true,
automaticRefresh: true,
expirationThreshold: 3600,
},
middleware: {
globalMiddlewareEnabled: true,
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ export async function requireUserSession(event: H3Event) {
})
}

const expired = persistentSession?.exp <= Math.trunc(Date.now() / 1000) // TODO: Add expiration threshold
const expired = persistentSession?.exp <= (Math.trunc(Date.now() / 1000) + (sessionConfig.expirationThreshold && typeof sessionConfig.expirationThreshold === 'number' ? sessionConfig.expirationThreshold : 0))
if (expired) {
logger.warn('Session expired')
logger.info('Session expired')
// Automatic token refresh
if (sessionConfig.automaticRefresh) {
await refreshUserSession(event)
Expand Down
11 changes: 8 additions & 3 deletions src/runtime/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ export interface AuthSessionConfig {
*/
expirationCheck?: boolean
/**
* Maximum auth session duration in seconds
* @default 60 * 60 * 24 (3600 = 1 day)
* Amount of seconds before access token expiration to trigger automatic refresh
* @default 0
*/
expirationThreshold?: number
/**
* Maximum auth session duration in seconds. Will be refreshed if session is refreshed
* @default 60 * 60 * 24 (86,400 = 1 day)
*/
maxAge?: number
/**
Expand All @@ -45,7 +50,7 @@ export interface AuthSessionConfig {
*/
sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined
/**
* Cookie secure attribute - Consider setting to true for production, would enforce https only cookies
* Cookie secure attribute - Consider setting to true for production, enforces https only cookies
* @default process.env.NODE_ENV === 'production'
*/
secure?: boolean | undefined
Expand Down

0 comments on commit 9933b8e

Please sign in to comment.