Add SPA to the Drive project for further usage

This commit is contained in:
2025-07-25 22:47:23 +08:00
parent d13fb8b0e4
commit 123dce564c
41 changed files with 2311 additions and 8 deletions

View File

@@ -0,0 +1,3 @@
import { defineStore } from 'pinia'
export const useServicesStore = defineStore('services', () => {})

View File

@@ -0,0 +1,59 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export const useUserStore = defineStore('user', () => {
// State
const user = ref<any>(null)
const isLoading = ref(false)
const error = ref<string | null>(null)
// Getters
const isAuthenticated = computed(() => !!user.value)
// Actions
async function fetchUser() {
isLoading.value = true
error.value = null
try {
const response = await fetch('/api/accounts/me', {
credentials: 'include',
})
if (!response.ok) {
// If the token is invalid, clear it and the user state
if (response.status === 401) {
logout()
}
throw new Error('Failed to fetch user information.')
}
user.value = await response.json()
} catch (e: any) {
error.value = e.message
user.value = null // Clear user data on error
} finally {
isLoading.value = false
}
}
function logout() {
user.value = null
localStorage.removeItem('authToken')
// Optionally, redirect to login page
// router.push('/login')
}
async function initialize() {
await fetchUser()
}
return {
user,
isLoading,
error,
isAuthenticated,
fetchUser,
logout,
initialize,
}
})