diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 5bbe13f6..ded55f69 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -2,6 +2,10 @@ export default defineNuxtConfig({ // ssr: false, extends: ['@nuxt/ui-pro'], modules: ['nuxt-auth-utils', '@nuxt/ui'], + auth: { + webAuthn: true, + autoExtendSession: true, + }, imports: { autoImport: true, }, @@ -22,7 +26,4 @@ export default defineNuxtConfig({ database: true, }, }, - auth: { - webAuthn: true, - }, }) diff --git a/src/module.ts b/src/module.ts index b5103e66..64d8df74 100644 --- a/src/module.ts +++ b/src/module.ts @@ -22,6 +22,11 @@ export interface ModuleOptions { * @default false */ webAuthn?: boolean + /** + * Auto extend session + * @default true + */ + autoExtendSession?: boolean /** * Hash options used for password hashing */ @@ -49,6 +54,7 @@ export default defineNuxtModule({ // Default configuration options of the Nuxt module defaults: { webAuthn: false, + autoExtendSession: true, hash: { scrypt: {}, }, @@ -142,6 +148,7 @@ export default defineNuxtModule({ register: {}, authenticate: {}, }) + runtimeConfig.autoExtendSession = options.autoExtendSession // OAuth settings runtimeConfig.oauth = defu(runtimeConfig.oauth, {}) diff --git a/src/runtime/server/plugins/oauth.ts b/src/runtime/server/plugins/oauth.ts index 4a26b245..99fea3cf 100644 --- a/src/runtime/server/plugins/oauth.ts +++ b/src/runtime/server/plugins/oauth.ts @@ -1,7 +1,24 @@ import type { NitroApp } from 'nitropack' import { defineNitroPlugin } from 'nitropack/runtime' +import { getUserSession, setUserSession } from '../utils/session' +import { useRuntimeConfig } from '#imports' export default defineNitroPlugin((nitroApp: NitroApp) => { + const config = useRuntimeConfig() + // update the session + if (config.autoExtendSession) { + nitroApp.hooks.hook('request', async (event) => { + if (event.path === '/api/_auth/session') { + return + } + + const session = await getUserSession(event) + if (session && Object.keys(session).length > 0) { + await setUserSession(event, session) + } + }) + } + if ( (process.env.NUXT_OAUTH_FACEBOOK_CLIENT_ID && process.env.NUXT_OAUTH_FACEBOOK_CLIENT_SECRET) || (process.env.NUXT_OAUTH_INSTAGRAM_CLIENT_ID && process.env.NUXT_OAUTH_INSTAGRAM_CLIENT_SECRET)