Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto extend session cookie #255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -22,7 +26,4 @@ export default defineNuxtConfig({
database: true,
},
},
auth: {
webAuthn: true,
},
})
7 changes: 7 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface ModuleOptions {
* @default false
*/
webAuthn?: boolean
/**
* Auto extend session
* @default true
*/
autoExtendSession?: boolean
/**
* Hash options used for password hashing
*/
Expand Down Expand Up @@ -49,6 +54,7 @@ export default defineNuxtModule<ModuleOptions>({
// Default configuration options of the Nuxt module
defaults: {
webAuthn: false,
autoExtendSession: true,
hash: {
scrypt: {},
},
Expand Down Expand Up @@ -142,6 +148,7 @@ export default defineNuxtModule<ModuleOptions>({
register: {},
authenticate: {},
})
runtimeConfig.autoExtendSession = options.autoExtendSession

// OAuth settings
runtimeConfig.oauth = defu(runtimeConfig.oauth, {})
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/server/plugins/oauth.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down