diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 668f5ac..2b82d5a 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -28,5 +28,5 @@ export default defineNuxtConfig({ }, }, - compatibilityDate: '2024-08-30', + compatibilityDate: '2024-09-28', }) diff --git a/src/runtime/httpFactory.ts b/src/runtime/httpFactory.ts index 17a2507..e3a1510 100644 --- a/src/runtime/httpFactory.ts +++ b/src/runtime/httpFactory.ts @@ -9,7 +9,7 @@ import handleRequestHeaders from './interceptors/common/request' import handleRequestTokenHeader from './interceptors/token/request' import type { SanctumAppConfig, SanctumInterceptor } from './types/config' import type { ModuleOptions } from './types/options' -import { navigateTo, useNuxtApp } from '#app' +import { navigateTo, type NuxtApp } from '#app' function configureClientInterceptors( requestInterceptors: SanctumInterceptor[], @@ -46,11 +46,10 @@ function determineCredentialsMode() { return 'include' } -export function createHttpClient(logger: ConsolaInstance): $Fetch { +export function createHttpClient(nuxtApp: NuxtApp, logger: ConsolaInstance): $Fetch { const options = useSanctumConfig() const user = useSanctumUser() const appConfig = useSanctumAppConfig() - const nuxtApp = useNuxtApp() const requestInterceptors: SanctumInterceptor[] = [handleRequestHeaders] const responseInterceptors: SanctumInterceptor[] = [] diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 36d44e6..6ece1e8 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -6,7 +6,7 @@ import { useSanctumConfig } from './composables/useSanctumConfig' import { useSanctumAppConfig } from './composables/useSanctumAppConfig' import type { ModuleOptions } from './types/options' import { IDENTITY_LOADED_KEY } from './utils/constants' -import { defineNuxtPlugin, updateAppConfig, useState } from '#app' +import { defineNuxtPlugin, updateAppConfig, useState, type NuxtApp } from '#app' const LOGGER_NAME = 'nuxt-auth-sanctum' @@ -17,17 +17,19 @@ function createSanctumLogger(logLevel: number) { return createConsola({ level: logLevel }).withTag(loggerName) } -async function setupDefaultTokenStorage(logger: ConsolaInstance) { +async function setupDefaultTokenStorage(nuxtApp: NuxtApp, logger: ConsolaInstance) { logger.debug( 'Token storage is not defined, switch to default cookie storage', ) const defaultStorage = await import('./storages/cookieTokenStorage') - updateAppConfig({ - sanctum: { - tokenStorage: defaultStorage.cookieTokenStorage, - }, + nuxtApp.runWithContext(() => { + updateAppConfig({ + sanctum: { + tokenStorage: defaultStorage.cookieTokenStorage, + }, + }) }) } @@ -39,7 +41,7 @@ async function initialIdentityLoad(client: $Fetch, options: ModuleOptions, logge () => false, ) - if (user.value === null && identityFetchedOnInit.value === false) { + if (user.value === null && !identityFetchedOnInit.value) { identityFetchedOnInit.value = true try { @@ -68,14 +70,15 @@ function handleIdentityLoadError(error: Error, logger: ConsolaInstance) { } } -export default defineNuxtPlugin(async () => { +export default defineNuxtPlugin(async (_nuxtApp) => { + const nuxtApp = _nuxtApp as NuxtApp const options = useSanctumConfig() const appConfig = useSanctumAppConfig() const logger = createSanctumLogger(options.logLevel) - const client = createHttpClient(logger) + const client = createHttpClient(nuxtApp, logger) if (options.mode === 'token' && !appConfig.tokenStorage) { - await setupDefaultTokenStorage(logger) + await setupDefaultTokenStorage(nuxtApp, logger) } if (options.client.initialRequest) {