Skip to content

Commit

Permalink
feat: modify plugin to fetch data when needed on app/page load.
Browse files Browse the repository at this point in the history
  • Loading branch information
codiam committed Apr 27, 2023
1 parent 4195895 commit 764d02e
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
import { useDirectusAuth } from './composables/useDirectusAuth'

import { useDirectusToken } from './composables/useDirectusToken';
import { useDirectusUser } from './composables/useDirectusUser';
import { useDirectusAuth } from './composables/useDirectusAuth';

export default defineNuxtPlugin(async (nuxtApp) => {
const config = useRuntimeConfig()
if (config.public.directus.autoFetch) {
const { fetchUser } = useDirectusAuth()

await fetchUser()
const config = useRuntimeConfig();
const { fetchUser } = useDirectusAuth();
const { token } = useDirectusToken();
const user = useDirectusUser();

async function checkIfUserExists() {
if (config.public.directus.autoFetch) {
if (!user.value && token.value) {
await fetchUser();
}
}
}
})

nuxtApp.hook('app:created', async () => {
if (process.server) {
await checkIfUserExists();
}
})

nuxtApp.hook('page:start', async () => {
if (process.client) {
await checkIfUserExists();
}
})
})

0 comments on commit 764d02e

Please sign in to comment.