Fix useContent not using cookies therefore skipping the auth step

This commit is contained in:
2025-01-05 22:43:40 +01:00
parent 9515132659
commit 896af11fa7
15 changed files with 37 additions and 39 deletions

View File

@@ -22,7 +22,7 @@ export function useContent(): ContentComposable {
async function fetch(force: boolean) {
const content = useContentState();
if(content.value.length === 0 || force)
content.value = await $fetch('/api/file/overview');
content.value = await useRequestFetch()('/api/file/overview');
}
async function get(path: string) {
@@ -31,7 +31,7 @@ async function get(path: string) {
const item = value.find(e => e.path === path);
if(item)
{
item.content = await $fetch(`/api/file/content/${encodeURIComponent(path)}`);
item.content = await useRequestFetch()(`/api/file/content/${encodeURIComponent(path)}`);
}
content.value = value;

View File

@@ -1,7 +1,9 @@
import type { UserSession, UserSessionComposable } from '~/types/auth'
import { useContent } from './useContent'
const useSessionState = () => useState<UserSession>('nuxt-session', () => ({}))
const useAuthReadyState = () => useState('nuxt-auth-ready', () => false)
const useContentFetch = (force: boolean) => useContent().fetch(force);
/**
* Composable to get back the user session and utils around it.
@@ -13,28 +15,27 @@ export function useUserSession(): UserSessionComposable {
return {
ready: computed(() => authReadyState.value),
loggedIn: computed(() => Boolean(sessionState.value.user)),
user: computed(() => sessionState.value.user || null),
user: computed(() => sessionState.value.user ?? null),
session: sessionState,
fetch,
clear,
}
}
async function fetch() {
async function fetch(): Promise<boolean> {
const authReadyState = useAuthReadyState()
useSessionState().value = await useRequestFetch()('/api/auth/session', {
headers: {
Accept: 'text/json',
},
retry: false,
}).catch(() => ({}))
if (!authReadyState.value) {
const sessionState = useSessionState()
const loggedIn = Boolean(sessionState.value.user)
sessionState.value = await useRequestFetch()('/api/auth/session').catch(() => ({}))
if (!authReadyState.value)
{
authReadyState.value = true
}
return loggedIn !== Boolean(sessionState.value.user);
}
async function clear() {
await $fetch('/api/auth/session', { method: 'DELETE' })
await useRequestFetch()('/api/auth/session', { method: 'DELETE' })
useSessionState().value = {}
useRouter().go(0);
useRouter().go(0)
}