优化auth.ts
This commit is contained in:
parent
7599a5a670
commit
0161eee59f
@ -24,6 +24,7 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const loading = ref(false)
|
||||
const isAuthenticated = ref(false)
|
||||
const workspaces = ref<Array<{ name: string; label: string }>>([])
|
||||
const initPromise = ref<Promise<void> | 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 () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user