优化auth.ts

This commit is contained in:
jingrow 2026-03-15 22:42:03 +08:00
parent 7599a5a670
commit 0161eee59f

View File

@ -24,6 +24,7 @@ export const useAuthStore = defineStore('auth', () => {
const loading = ref(false) const loading = ref(false)
const isAuthenticated = ref(false) const isAuthenticated = ref(false)
const workspaces = ref<Array<{ name: string; label: string }>>([]) const workspaces = ref<Array<{ name: string; label: string }>>([])
const initPromise = ref<Promise<void> | null>(null)
const isLoggedIn = computed(() => isAuthenticated.value && !!user.value) const isLoggedIn = computed(() => isAuthenticated.value && !!user.value)
@ -84,28 +85,27 @@ export const useAuthStore = defineStore('auth', () => {
if (typeof window !== 'undefined' && (window as any).jingrow?.boot) { if (typeof window !== 'undefined' && (window as any).jingrow?.boot) {
(window as any).jingrow.boot.user = { name: 'Guest' } (window as any).jingrow.boot.user = { name: 'Guest' }
} }
initPromise.value = null
} }
} }
const initAuth = async () => { const initAuth = async () => {
// Initialize window.jingrow.boot if not exists if (initPromise.value) return initPromise.value
initPromise.value = (async () => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
if (!(window as any).jingrow) (window as any).jingrow = {} if (!(window as any).jingrow) (window as any).jingrow = {}
if (!(window as any).jingrow.boot) (window as any).jingrow.boot = {} if (!(window as any).jingrow.boot) (window as any).jingrow.boot = {}
} }
// Check user_id cookie first
const cookies = new URLSearchParams(document.cookie.split('; ').join('&')) const cookies = new URLSearchParams(document.cookie.split('; ').join('&'))
const userId = cookies.get('user_id') const userId = cookies.get('user_id')
if (userId === 'Guest' || !userId) { if (userId === 'Guest' || !userId) return
return
}
// Try to get boot info from server-side injection first (production)
let bootUser = (window as any).jingrow?.boot?.user let bootUser = (window as any).jingrow?.boot?.user
// If not available (dev mode), fetch from API
if (!bootUser || bootUser.name === 'Guest') { if (!bootUser || bootUser.name === 'Guest') {
try { try {
const response = await fetch('/api/action/jingrow.sessions.get_boot_info', { const response = await fetch('/api/action/jingrow.sessions.get_boot_info', {
@ -115,10 +115,8 @@ export const useAuthStore = defineStore('auth', () => {
}) })
if (response.ok) { if (response.ok) {
const result = await response.json() const result = await response.json()
const bootInfo = result.message ;(window as any).jingrow.boot = result.message
// Store boot info globally bootUser = result.message.user
;(window as any).jingrow.boot = bootInfo
bootUser = bootInfo.user
} }
} catch (e) { } catch (e) {
// Failed to fetch boot info // Failed to fetch boot info
@ -142,7 +140,6 @@ export const useAuthStore = defineStore('auth', () => {
} }
isAuthenticated.value = true isAuthenticated.value = true
// Initialize slug mapping from user's can_read list
if (bootUser.can_read?.length) { if (bootUser.can_read?.length) {
initSlugMapping(bootUser.can_read) initSlugMapping(bootUser.can_read)
} }
@ -151,7 +148,6 @@ export const useAuthStore = defineStore('auth', () => {
localStorage.setItem('jingrow_authenticated', 'true') 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) { if ((window as any).jingrow?.boot?.setup_complete === undefined) {
try { try {
const response = await fetch('/api/data/System%20Settings/System%20Settings?fields=["setup_complete"]') const response = await fetch('/api/data/System%20Settings/System%20Settings?fields=["setup_complete"]')
@ -160,9 +156,12 @@ export const useAuthStore = defineStore('auth', () => {
;(window as any).jingrow.boot.setup_complete = data?.data?.setup_complete ;(window as any).jingrow.boot.setup_complete = data?.data?.setup_complete
} }
} catch (e) { } catch (e) {
// API call failed, continue without setup_complete // API call failed
} }
} }
})()
return initPromise.value
} }
const updateUserInfo = async () => { const updateUserInfo = async () => {