Skip to content

Commit e329961

Browse files
feat: allow disabling session auto-loading
See #309
1 parent b25dae7 commit e329961

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ declare module 'nuxt/schema' {
5050
* Session configuration
5151
*/
5252
session: SessionConfig
53+
disableAuthAutoLoad?: boolean
5354
}
5455
}
5556

@@ -495,5 +496,10 @@ export default defineNuxtModule<ModuleOptions>({
495496
tokenURL: '',
496497
userURL: '',
497498
})
499+
500+
// Publicly expose disableAuthAutoLoad if needed
501+
if (runtimeConfig.disableAuthAutoLoad && runtimeConfig.public.disableAuthAutoLoad === undefined) {
502+
runtimeConfig.public.disableAuthAutoLoad = runtimeConfig.disableAuthAutoLoad
503+
}
498504
},
499505
})

src/runtime/app/plugins/session.client.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// TODO: https://github.com/nuxt/module-builder/issues/141
2-
import {} from 'nuxt/app'
32
import { defineNuxtPlugin, useUserSession, useError } from '#imports'
43

54
export default defineNuxtPlugin(async (nuxtApp) => {
6-
if (!nuxtApp.payload.serverRendered) {
7-
await useUserSession().fetch()
8-
}
9-
else if (Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
10-
// To avoid hydration mismatch
11-
nuxtApp.hook('app:mounted', async () => {
5+
if (!nuxtApp.$config.public.disableAuthAutoLoad) {
6+
if (!nuxtApp.payload.serverRendered) {
127
await useUserSession().fetch()
13-
})
8+
}
9+
else if (Boolean(nuxtApp.payload.prerenderedAt) || Boolean(nuxtApp.payload.isCached)) {
10+
// To avoid hydration mismatch
11+
nuxtApp.hook('app:mounted', async () => {
12+
await useUserSession().fetch()
13+
})
14+
}
1415
}
1516

1617
if (localStorage.getItem('temp-nuxt-auth-utils-popup')) {

src/runtime/app/plugins/session.server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// TODO: https://github.com/nuxt/module-builder/issues/141
2-
import {} from 'nuxt/app'
32
import { defineNuxtPlugin, useRequestEvent, useUserSession } from '#imports'
43

54
export default defineNuxtPlugin({
@@ -8,7 +7,7 @@ export default defineNuxtPlugin({
87
async setup(nuxtApp) {
98
// Flag if request is cached
109
nuxtApp.payload.isCached = Boolean(useRequestEvent()?.context.cache)
11-
if (nuxtApp.payload.serverRendered && !nuxtApp.payload.prerenderedAt && !nuxtApp.payload.isCached) {
10+
if (nuxtApp.payload.serverRendered && !nuxtApp.payload.prerenderedAt && !nuxtApp.payload.isCached && !nuxtApp.$config.disableAuthAutoLoad) {
1211
await useUserSession().fetch()
1312
}
1413
},

0 commit comments

Comments
 (0)