diff --git a/package.json b/package.json index adc69e0..e56dbe7 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "test": "vitest run", "test:watch": "vitest watch", "validate": "npm run fmt:check && npm run lint && npm run types && npm run test", - "release": "npm run validate && npm run prepack && changelogen --release && npm publish" + "release": "npm run validate && npm run prepack && changelogen --release && npm publish && git push" }, "dependencies": { "@nuxt/kit": "^3.9.0", diff --git a/src/runtime/httpFactory.ts b/src/runtime/httpFactory.ts index 092c9d8..349c074 100644 --- a/src/runtime/httpFactory.ts +++ b/src/runtime/httpFactory.ts @@ -147,7 +147,8 @@ export function createHttpClient(logger: ConsolaInstance): $Fetch { if ( response.status === 401 && - request.toString().endsWith(options.endpoints.user) + request.toString().endsWith(options.endpoints.user) && + user.value !== null ) { logger.warn( 'User session is not set in API or expired, resetting identity' diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 569f194..15655d1 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -1,5 +1,5 @@ import { FetchError } from 'ofetch'; -import { defineNuxtPlugin } from '#app'; +import { defineNuxtPlugin, useState } from '#app'; import { createHttpClient } from './httpFactory'; import { useSanctumUser } from './composables/useSanctumUser'; import { useSanctumConfig } from './composables/useSanctumConfig'; @@ -34,7 +34,14 @@ export default defineNuxtPlugin(async () => { const logger = createSanctumLogger(options.logLevel); const client = createHttpClient(logger); - if (user.value === null) { + const identityFetchedOnInit = useState( + 'sanctum.user.loaded', + () => false + ); + + if (user.value === null && identityFetchedOnInit.value === false) { + identityFetchedOnInit.value = true; + try { user.value = await client(options.endpoints.user); } catch (error) {