import { defineNuxtPlugin, useRuntimeConfig } from '#app'
export default defineNuxtPlugin(async (nuxtApp) => {
const config = useRuntimeConfig();
const { fetchUser } = useAuth();
const { token, checkAutoRefresh } = useToken();
const user = useUser();
async function checkIfUserExists() {
if (config.public.autoFetch) {
if (!user.value && token.value) {
await fetchUser();
}
}
}
// do the checks server-side, instead of using hook 'app:created',
// as this hook is not called on SSR=true (static generation)
await checkAutoRefresh();
await checkIfUserExists();
nuxtApp.hook('page:start', async () => {
if (import.meta.client) {
await checkAutoRefresh();
await checkIfUserExists();
}
})
})