优化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 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,85 +85,83 @@ 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
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
if (!(window as any).jingrow) (window as any).jingrow = {}
|
|
||||||
if (!(window as any).jingrow.boot) (window as any).jingrow.boot = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check user_id cookie first
|
initPromise.value = (async () => {
|
||||||
const cookies = new URLSearchParams(document.cookie.split('; ').join('&'))
|
if (typeof window !== 'undefined') {
|
||||||
const userId = cookies.get('user_id')
|
if (!(window as any).jingrow) (window as any).jingrow = {}
|
||||||
|
if (!(window as any).jingrow.boot) (window as any).jingrow.boot = {}
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.setItem('jingrow_user', JSON.stringify(user.value))
|
const cookies = new URLSearchParams(document.cookie.split('; ').join('&'))
|
||||||
localStorage.setItem('jingrow_authenticated', 'true')
|
const userId = cookies.get('user_id')
|
||||||
}
|
|
||||||
|
if (userId === 'Guest' || !userId) return
|
||||||
// Fetch setup_complete status if not already set (for dev mode)
|
|
||||||
if ((window as any).jingrow?.boot?.setup_complete === undefined) {
|
let bootUser = (window as any).jingrow?.boot?.user
|
||||||
try {
|
|
||||||
const response = await fetch('/api/data/System%20Settings/System%20Settings?fields=["setup_complete"]')
|
if (!bootUser || bootUser.name === 'Guest') {
|
||||||
if (response.ok) {
|
try {
|
||||||
const data = await response.json()
|
const response = await fetch('/api/action/jingrow.sessions.get_boot_info', {
|
||||||
;(window as any).jingrow.boot.setup_complete = data?.data?.setup_complete
|
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 () => {
|
const updateUserInfo = async () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user