Skip to content

Commit

Permalink
Merge pull request #179 from manchenkoff/176-token-mode-error
Browse files Browse the repository at this point in the history
fix: use nuxt app context for default token storage
  • Loading branch information
manchenkoff authored Sep 28, 2024
2 parents 3687455 + 1f7fdb2 commit b96632f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export default defineNuxtConfig({
},
},

compatibilityDate: '2024-08-30',
compatibilityDate: '2024-09-28',
})
5 changes: 2 additions & 3 deletions src/runtime/httpFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -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[] = []
Expand Down
23 changes: 13 additions & 10 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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,
},
})
})
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b96632f

Please sign in to comment.