From 0161eee59f2b7e594a1832325a91e783222a9ee2 Mon Sep 17 00:00:00 2001 From: jingrow Date: Sun, 15 Mar 2026 22:42:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96auth.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontend/src/shared/stores/auth.ts | 139 +++++++++--------- 1 file changed, 69 insertions(+), 70 deletions(-) diff --git a/apps/jingrow/frontend/src/shared/stores/auth.ts b/apps/jingrow/frontend/src/shared/stores/auth.ts index b9f0444..d793cc2 100644 --- a/apps/jingrow/frontend/src/shared/stores/auth.ts +++ b/apps/jingrow/frontend/src/shared/stores/auth.ts @@ -24,6 +24,7 @@ export const useAuthStore = defineStore('auth', () => { const loading = ref(false) const isAuthenticated = ref(false) const workspaces = ref>([]) + const initPromise = ref | null>(null) const isLoggedIn = computed(() => isAuthenticated.value && !!user.value) @@ -84,85 +85,83 @@ export const useAuthStore = defineStore('auth', () => { if (typeof window !== 'undefined' && (window as any).jingrow?.boot) { (window as any).jingrow.boot.user = { name: 'Guest' } } + + initPromise.value = null } } const initAuth = async () => { - // Initialize window.jingrow.boot if not exists - if (typeof window !== 'undefined') { - if (!(window as any).jingrow) (window as any).jingrow = {} - if (!(window as any).jingrow.boot) (window as any).jingrow.boot = {} - } + if (initPromise.value) return initPromise.value - // Check user_id cookie first - const cookies = new URLSearchParams(document.cookie.split('; ').join('&')) - const userId = cookies.get('user_id') - - if (userId === 'Guest' || !userId) { - return - } - - // Try to get boot info from server-side injection first (production) - let bootUser = (window as any).jingrow?.boot?.user - - // If not available (dev mode), fetch from API - if (!bootUser || bootUser.name === 'Guest') { - try { - const response = await fetch('/api/action/jingrow.sessions.get_boot_info', { - method: 'GET', - headers: { 'Accept': 'application/json' }, - credentials: 'include' - }) - if (response.ok) { - const result = await response.json() - const bootInfo = result.message - // Store boot info globally - ;(window as any).jingrow.boot = bootInfo - bootUser = bootInfo.user - } - } catch (e) { - // Failed to fetch boot info - } - } - - if (bootUser && bootUser.name !== 'Guest') { - user.value = { - id: bootUser.name || bootUser.user || '', - name: bootUser.name || bootUser.user || '', - username: bootUser.name || bootUser.user || '', - email: bootUser.email || '', - avatar: bootUser.user_image || '', - first_name: bootUser.first_name || '', - last_name: bootUser.last_name || '', - user_type: bootUser.user_type || 'System User', - recent: bootUser.recent || '[]', - can_read: bootUser.can_read || [], - can_create: bootUser.can_create || [], - can_search: bootUser.can_search || [] - } - isAuthenticated.value = true - - // Initialize slug mapping from user's can_read list - if (bootUser.can_read?.length) { - initSlugMapping(bootUser.can_read) + initPromise.value = (async () => { + if (typeof window !== 'undefined') { + if (!(window as any).jingrow) (window as any).jingrow = {} + if (!(window as any).jingrow.boot) (window as any).jingrow.boot = {} } - localStorage.setItem('jingrow_user', JSON.stringify(user.value)) - localStorage.setItem('jingrow_authenticated', 'true') - } - - // Fetch setup_complete status if not already set (for dev mode) - if ((window as any).jingrow?.boot?.setup_complete === undefined) { - try { - const response = await fetch('/api/data/System%20Settings/System%20Settings?fields=["setup_complete"]') - if (response.ok) { - const data = await response.json() - ;(window as any).jingrow.boot.setup_complete = data?.data?.setup_complete + const cookies = new URLSearchParams(document.cookie.split('; ').join('&')) + const userId = cookies.get('user_id') + + if (userId === 'Guest' || !userId) return + + let bootUser = (window as any).jingrow?.boot?.user + + if (!bootUser || bootUser.name === 'Guest') { + try { + const response = await fetch('/api/action/jingrow.sessions.get_boot_info', { + method: 'GET', + headers: { 'Accept': 'application/json' }, + credentials: 'include' + }) + if (response.ok) { + const result = await response.json() + ;(window as any).jingrow.boot = result.message + bootUser = result.message.user + } + } catch (e) { + // Failed to fetch boot info } - } catch (e) { - // API call failed, continue without setup_complete } - } + + if (bootUser && bootUser.name !== 'Guest') { + user.value = { + id: bootUser.name || bootUser.user || '', + name: bootUser.name || bootUser.user || '', + username: bootUser.name || bootUser.user || '', + email: bootUser.email || '', + avatar: bootUser.user_image || '', + first_name: bootUser.first_name || '', + last_name: bootUser.last_name || '', + user_type: bootUser.user_type || 'System User', + recent: bootUser.recent || '[]', + can_read: bootUser.can_read || [], + can_create: bootUser.can_create || [], + can_search: bootUser.can_search || [] + } + isAuthenticated.value = true + + if (bootUser.can_read?.length) { + initSlugMapping(bootUser.can_read) + } + + localStorage.setItem('jingrow_user', JSON.stringify(user.value)) + localStorage.setItem('jingrow_authenticated', 'true') + } + + if ((window as any).jingrow?.boot?.setup_complete === undefined) { + try { + const response = await fetch('/api/data/System%20Settings/System%20Settings?fields=["setup_complete"]') + if (response.ok) { + const data = await response.json() + ;(window as any).jingrow.boot.setup_complete = data?.data?.setup_complete + } + } catch (e) { + // API call failed + } + } + })() + + return initPromise.value } const updateUserInfo = async () => {