Skip to content

feat: allow disabling session auto-loading #435

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

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
6 changes: 6 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare module 'nuxt/schema' {
* Session configuration
*/
session: SessionConfig
disableAuthAutoLoad?: boolean
}
}

Expand Down Expand Up @@ -468,5 +469,10 @@ export default defineNuxtModule<ModuleOptions>({
redirectURL: '',
scope: '',
})

// Publicly expose disableAuthAutoLoad if needed
if (runtimeConfig.disableAuthAutoLoad && runtimeConfig.public.disableAuthAutoLoad === undefined) {
runtimeConfig.public.disableAuthAutoLoad = runtimeConfig.disableAuthAutoLoad
}
},
})
17 changes: 9 additions & 8 deletions src/runtime/app/plugins/session.client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// TODO: https://github.com/nuxt/module-builder/issues/141
import {} from 'nuxt/app'
import { defineNuxtPlugin, useUserSession, useError } from '#imports'

export default defineNuxtPlugin(async (nuxtApp) => {
if (!nuxtApp.payload.serverRendered) {
await useUserSession().fetch()
}
else if (Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
// To avoid hydration mismatch
nuxtApp.hook('app:mounted', async () => {
if (!nuxtApp.$config.public.disableAuthAutoLoad) {
if (!nuxtApp.payload.serverRendered) {
await useUserSession().fetch()
})
}
else if (Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
// To avoid hydration mismatch
nuxtApp.hook('app:mounted', async () => {
await useUserSession().fetch()
})
}
}

if (localStorage.getItem('temp-nuxt-auth-utils-popup')) {
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/app/plugins/session.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: https://github.com/nuxt/module-builder/issues/141
import {} from 'nuxt/app'
import { defineNuxtPlugin, useRequestEvent, useUserSession } from '#imports'

export default defineNuxtPlugin({
Expand All @@ -8,7 +7,7 @@ export default defineNuxtPlugin({
async setup(nuxtApp) {
// Flag if request is cached
nuxtApp.payload.isCached = Boolean(useRequestEvent()?.context.cache)
if (nuxtApp.payload.serverRendered && !nuxtApp.payload.prerenderedAt && !nuxtApp.payload.isCached) {
if (nuxtApp.payload.serverRendered && !nuxtApp.payload.prerenderedAt && !nuxtApp.payload.isCached && !nuxtApp.$config.disableAuthAutoLoad) {
await useUserSession().fetch()
}
},
Expand Down