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 2 commits 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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,18 @@ This is because the user session is stored in a secure cookie and cannot be acce

**This means that you should not rely on the user session during prerendering.**

You may also choose to instruct Nuxt AUth Utils to not fetch automatically the user session, with the `disableAutoLoad` option in your `nuxt.config.ts`:

```ts
export default defineNuxtConfig({
auth: {
disableAutoLoad: true
}
})
```

When using the `disableAutoLoad` option, the user session should be manually fetched by calling `fetch` from the `useUserSession` composable.

### `<AuthState>` component

You can use the `<AuthState>` component to safely display auth-related data in your components without worrying about the rendering mode.
Expand All @@ -617,7 +629,7 @@ One common use case if the Login button in the header:
</template>
```

If the page is cached or prerendered, nothing will be rendered until the user session is fetched on the client-side.
If the page is cached or prerendered, or the auto-loading of the session disabled and the session not manually fetched on the server side, nothing will be rendered until the user session is fetched on the client-side.

You can use the `placeholder` slot to show a placeholder on server-side and while the user session is being fetched on client-side for the prerendered pages:

Expand Down
16 changes: 16 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export interface ModuleOptions {
*/
scrypt?: ScryptConfig
}
/**
* Disable automatically fething the session
*/
disableAutoLoad?: boolean
}

declare module 'nuxt/schema' {
Expand All @@ -51,6 +55,11 @@ declare module 'nuxt/schema' {
*/
session: SessionConfig
}
interface PublicRuntimeConfig {
auth: {
disableAutoLoad?: boolean
}
}
}

export default defineNuxtModule<ModuleOptions>({
Expand Down Expand Up @@ -162,6 +171,13 @@ export default defineNuxtModule<ModuleOptions>({
}
}

// Publicly expose disableAutoLoad if needed
if (options.disableAutoLoad && runtimeConfig.public.auth?.disableAutoLoad === undefined) {
runtimeConfig.public.auth = defu(runtimeConfig.public.auth, {
disableAutoLoad: options.disableAutoLoad,
})
}

// WebAuthn settings
runtimeConfig.webauthn = defu(runtimeConfig.webauthn, {
register: {},
Expand Down
16 changes: 9 additions & 7 deletions src/runtime/app/plugins/session.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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.auth.disableAutoLoad) {
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
4 changes: 3 additions & 1 deletion src/runtime/app/plugins/session.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ 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.public.auth.disableAutoLoad
) {
await useUserSession().fetch()
}
},
Expand Down