feat: add JWT authentication and AGENTS.md

This commit is contained in:
祀梦
2026-05-17 11:21:41 +08:00
parent 40eb2dadb0
commit 3c03866021
19 changed files with 554 additions and 1632 deletions

View File

@@ -0,0 +1,49 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { login as apiLogin } from '@/api/auth'
const TOKEN_KEY = 'elysia_auth_token'
function getStoredToken(): string {
return localStorage.getItem(TOKEN_KEY) || ''
}
function setStoredToken(token: string) {
localStorage.setItem(TOKEN_KEY, token)
}
function clearStoredToken() {
localStorage.removeItem(TOKEN_KEY)
}
export const useAuthStore = defineStore('auth', () => {
const token = ref<string>(getStoredToken())
const loading = ref(false)
const error = ref('')
const isLoggedIn = computed(() => !!token.value)
async function login(password: string): Promise<boolean> {
loading.value = true
error.value = ''
try {
const res = await apiLogin(password)
token.value = res.access_token
setStoredToken(res.access_token)
return true
} catch (e: any) {
error.value = e?.response?.data?.detail || '登录失败'
return false
} finally {
loading.value = false
}
}
function logout() {
token.value = ''
error.value = ''
clearStoredToken()
}
return { token, loading, error, isLoggedIn, login, logout }
})