Skip to content

Commit

Permalink
fix: request identity once on plugin init
Browse files Browse the repository at this point in the history
  • Loading branch information
manchenkoff committed Apr 17, 2024
1 parent d314b86 commit c88fbdf
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -34,7 +34,14 @@ export default defineNuxtPlugin(async () => {
const logger = createSanctumLogger(options.logLevel);
const client = createHttpClient(logger);

if (user.value === null) {
const identityFetchedOnInit = useState<boolean>(
'sanctum.user.loaded',
() => false
);

if (user.value === null && identityFetchedOnInit.value === false) {
identityFetchedOnInit.value = true;

try {
user.value = await client(options.endpoints.user);
} catch (error) {
Expand Down

0 comments on commit c88fbdf

Please sign in to comment.