2025-12-18 13:50:39 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Authentication Store
|
2026-02-05 12:38:48 +08:00
|
|
|
|
* Manages user authentication state, login/logout, token refresh, and token persistence
|
2025-12-18 13:50:39 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
2025-12-25 08:41:43 -08:00
|
|
|
|
import { defineStore } from 'pinia'
|
2025-12-29 03:17:25 +08:00
|
|
|
|
import { ref, computed, readonly } from 'vue'
|
2026-01-26 08:45:43 +08:00
|
|
|
|
import { authAPI, isTotp2FARequired, type LoginResponse } from '@/api'
|
|
|
|
|
|
import type { User, LoginRequest, RegisterRequest, AuthResponse } from '@/types'
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2025-12-25 08:41:43 -08:00
|
|
|
|
const AUTH_TOKEN_KEY = 'auth_token'
|
|
|
|
|
|
const AUTH_USER_KEY = 'auth_user'
|
2026-02-05 12:38:48 +08:00
|
|
|
|
const REFRESH_TOKEN_KEY = 'refresh_token'
|
|
|
|
|
|
const TOKEN_EXPIRES_AT_KEY = 'token_expires_at' // 存储过期时间戳而非有效期
|
|
|
|
|
|
const AUTO_REFRESH_INTERVAL = 60 * 1000 // 60 seconds for user data refresh
|
|
|
|
|
|
const TOKEN_REFRESH_BUFFER = 120 * 1000 // 120 seconds before expiry to refresh token
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
|
|
|
|
// ==================== State ====================
|
|
|
|
|
|
|
2025-12-25 08:41:43 -08:00
|
|
|
|
const user = ref<User | null>(null)
|
|
|
|
|
|
const token = ref<string | null>(null)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
const refreshTokenValue = ref<string | null>(null)
|
|
|
|
|
|
const tokenExpiresAt = ref<number | null>(null) // 过期时间戳(毫秒)
|
2025-12-29 03:17:25 +08:00
|
|
|
|
const runMode = ref<'standard' | 'simple'>('standard')
|
2025-12-25 08:41:43 -08:00
|
|
|
|
let refreshIntervalId: ReturnType<typeof setInterval> | null = null
|
2026-02-05 12:38:48 +08:00
|
|
|
|
let tokenRefreshTimeoutId: ReturnType<typeof setTimeout> | null = null
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
// ==================== Computed ====================
|
|
|
|
|
|
|
|
|
|
|
|
const isAuthenticated = computed(() => {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
return !!token.value && !!user.value
|
|
|
|
|
|
})
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
const isAdmin = computed(() => {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
return user.value?.role === 'admin'
|
|
|
|
|
|
})
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2025-12-29 03:17:25 +08:00
|
|
|
|
const isSimpleMode = computed(() => runMode.value === 'simple')
|
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
// ==================== Actions ====================
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Initialize auth state from localStorage
|
|
|
|
|
|
* Call this on app startup to restore session
|
|
|
|
|
|
* Also starts auto-refresh and immediately fetches latest user data
|
|
|
|
|
|
*/
|
|
|
|
|
|
function checkAuth(): void {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
const savedToken = localStorage.getItem(AUTH_TOKEN_KEY)
|
|
|
|
|
|
const savedUser = localStorage.getItem(AUTH_USER_KEY)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
const savedRefreshToken = localStorage.getItem(REFRESH_TOKEN_KEY)
|
|
|
|
|
|
const savedExpiresAt = localStorage.getItem(TOKEN_EXPIRES_AT_KEY)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
if (savedToken && savedUser) {
|
|
|
|
|
|
try {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
token.value = savedToken
|
|
|
|
|
|
user.value = JSON.parse(savedUser)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
refreshTokenValue.value = savedRefreshToken
|
|
|
|
|
|
tokenExpiresAt.value = savedExpiresAt ? parseInt(savedExpiresAt, 10) : null
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
// Immediately refresh user data from backend (async, don't block)
|
|
|
|
|
|
refreshUser().catch((error) => {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
console.error('Failed to refresh user on init:', error)
|
|
|
|
|
|
})
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Start auto-refresh interval for user data
|
2025-12-25 08:41:43 -08:00
|
|
|
|
startAutoRefresh()
|
2026-02-05 12:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
// Start proactive token refresh if we have refresh token and expiry info
|
|
|
|
|
|
// Note: use !== null to handle case when tokenExpiresAt.value is 0 (expired)
|
|
|
|
|
|
if (savedRefreshToken && tokenExpiresAt.value !== null) {
|
|
|
|
|
|
scheduleTokenRefreshAt(tokenExpiresAt.value)
|
|
|
|
|
|
}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
} catch (error) {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
console.error('Failed to parse saved user data:', error)
|
|
|
|
|
|
clearAuth()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Start auto-refresh interval for user data
|
|
|
|
|
|
* Refreshes user data every 60 seconds
|
|
|
|
|
|
*/
|
|
|
|
|
|
function startAutoRefresh(): void {
|
|
|
|
|
|
// Clear existing interval if any
|
2025-12-25 08:41:43 -08:00
|
|
|
|
stopAutoRefresh()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
refreshIntervalId = setInterval(() => {
|
|
|
|
|
|
if (token.value) {
|
|
|
|
|
|
refreshUser().catch((error) => {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
console.error('Auto-refresh user failed:', error)
|
|
|
|
|
|
})
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
2025-12-25 08:41:43 -08:00
|
|
|
|
}, AUTO_REFRESH_INTERVAL)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Stop auto-refresh interval
|
|
|
|
|
|
*/
|
|
|
|
|
|
function stopAutoRefresh(): void {
|
|
|
|
|
|
if (refreshIntervalId) {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
clearInterval(refreshIntervalId)
|
|
|
|
|
|
refreshIntervalId = null
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Schedule proactive token refresh before expiry (based on expiry timestamp)
|
|
|
|
|
|
* @param expiresAtMs - Token expiry timestamp in milliseconds
|
|
|
|
|
|
*/
|
|
|
|
|
|
function scheduleTokenRefreshAt(expiresAtMs: number): void {
|
|
|
|
|
|
// Clear any existing timeout
|
|
|
|
|
|
if (tokenRefreshTimeoutId) {
|
|
|
|
|
|
clearTimeout(tokenRefreshTimeoutId)
|
|
|
|
|
|
tokenRefreshTimeoutId = null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate remaining time until refresh (buffer time before expiry)
|
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
|
const refreshInMs = Math.max(0, expiresAtMs - now - TOKEN_REFRESH_BUFFER)
|
|
|
|
|
|
|
|
|
|
|
|
if (refreshInMs <= 0) {
|
|
|
|
|
|
// Token is about to expire or already expired, refresh immediately
|
|
|
|
|
|
performTokenRefresh()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tokenRefreshTimeoutId = setTimeout(() => {
|
|
|
|
|
|
performTokenRefresh()
|
|
|
|
|
|
}, refreshInMs)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Schedule proactive token refresh before expiry (based on expires_in seconds)
|
|
|
|
|
|
* @param expiresInSeconds - Token expiry time in seconds from now
|
|
|
|
|
|
*/
|
|
|
|
|
|
function scheduleTokenRefresh(expiresInSeconds: number): void {
|
|
|
|
|
|
const expiresAtMs = Date.now() + expiresInSeconds * 1000
|
|
|
|
|
|
tokenExpiresAt.value = expiresAtMs
|
|
|
|
|
|
localStorage.setItem(TOKEN_EXPIRES_AT_KEY, String(expiresAtMs))
|
|
|
|
|
|
scheduleTokenRefreshAt(expiresAtMs)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Perform the actual token refresh
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function performTokenRefresh(): Promise<void> {
|
|
|
|
|
|
if (!refreshTokenValue.value) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await authAPI.refreshToken()
|
|
|
|
|
|
|
|
|
|
|
|
// Update state
|
|
|
|
|
|
token.value = response.access_token
|
|
|
|
|
|
refreshTokenValue.value = response.refresh_token
|
|
|
|
|
|
|
|
|
|
|
|
// Schedule next refresh (this also updates tokenExpiresAt and localStorage)
|
|
|
|
|
|
scheduleTokenRefresh(response.expires_in)
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Token refresh failed:', error)
|
|
|
|
|
|
// Don't clear auth here - the interceptor will handle 401 errors
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Stop token refresh timeout
|
|
|
|
|
|
*/
|
|
|
|
|
|
function stopTokenRefresh(): void {
|
|
|
|
|
|
if (tokenRefreshTimeoutId) {
|
|
|
|
|
|
clearTimeout(tokenRefreshTimeoutId)
|
|
|
|
|
|
tokenRefreshTimeoutId = null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* User login
|
2026-01-26 08:45:43 +08:00
|
|
|
|
* @param credentials - Login credentials (email and password)
|
|
|
|
|
|
* @returns Promise resolving to the login response (may require 2FA)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
* @throws Error if login fails
|
|
|
|
|
|
*/
|
2026-01-26 08:45:43 +08:00
|
|
|
|
async function login(credentials: LoginRequest): Promise<LoginResponse> {
|
2025-12-18 13:50:39 +08:00
|
|
|
|
try {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
const response = await authAPI.login(credentials)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2026-01-26 08:45:43 +08:00
|
|
|
|
// If 2FA is required, return the response without setting auth state
|
|
|
|
|
|
if (isTotp2FARequired(response)) {
|
|
|
|
|
|
return response
|
2025-12-29 03:46:47 +08:00
|
|
|
|
}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2026-01-26 08:45:43 +08:00
|
|
|
|
// Set auth state from the response
|
|
|
|
|
|
setAuthFromResponse(response)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2026-01-26 08:45:43 +08:00
|
|
|
|
return response
|
2025-12-18 13:50:39 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
// Clear any partial state on error
|
2025-12-25 08:41:43 -08:00
|
|
|
|
clearAuth()
|
|
|
|
|
|
throw error
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 08:45:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Complete login with 2FA code
|
|
|
|
|
|
* @param tempToken - Temporary token from initial login
|
|
|
|
|
|
* @param totpCode - 6-digit TOTP code
|
|
|
|
|
|
* @returns Promise resolving to the authenticated user
|
|
|
|
|
|
* @throws Error if 2FA verification fails
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function login2FA(tempToken: string, totpCode: string): Promise<User> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await authAPI.login2FA({ temp_token: tempToken, totp_code: totpCode })
|
|
|
|
|
|
setAuthFromResponse(response)
|
|
|
|
|
|
return user.value!
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
clearAuth()
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Set auth state from an AuthResponse
|
|
|
|
|
|
* Internal helper function
|
|
|
|
|
|
*/
|
|
|
|
|
|
function setAuthFromResponse(response: AuthResponse): void {
|
|
|
|
|
|
// Store token and user
|
|
|
|
|
|
token.value = response.access_token
|
|
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Store refresh token if present
|
|
|
|
|
|
if (response.refresh_token) {
|
|
|
|
|
|
refreshTokenValue.value = response.refresh_token
|
|
|
|
|
|
localStorage.setItem(REFRESH_TOKEN_KEY, response.refresh_token)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 08:45:43 +08:00
|
|
|
|
// Extract run_mode if present
|
|
|
|
|
|
if (response.user.run_mode) {
|
|
|
|
|
|
runMode.value = response.user.run_mode
|
|
|
|
|
|
}
|
|
|
|
|
|
const { run_mode: _run_mode, ...userData } = response.user
|
|
|
|
|
|
user.value = userData
|
|
|
|
|
|
|
|
|
|
|
|
// Persist to localStorage
|
|
|
|
|
|
localStorage.setItem(AUTH_TOKEN_KEY, response.access_token)
|
|
|
|
|
|
localStorage.setItem(AUTH_USER_KEY, JSON.stringify(userData))
|
|
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Start auto-refresh interval for user data
|
2026-01-26 08:45:43 +08:00
|
|
|
|
startAutoRefresh()
|
2026-02-05 12:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
// Start proactive token refresh if we have refresh token and expiry info
|
|
|
|
|
|
// scheduleTokenRefresh will also store the expiry timestamp
|
|
|
|
|
|
if (response.refresh_token && response.expires_in) {
|
|
|
|
|
|
scheduleTokenRefresh(response.expires_in)
|
|
|
|
|
|
}
|
2026-01-26 08:45:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* User registration
|
|
|
|
|
|
* @param userData - Registration data (username, email, password)
|
|
|
|
|
|
* @returns Promise resolving to the newly registered and authenticated user
|
|
|
|
|
|
* @throws Error if registration fails
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function register(userData: RegisterRequest): Promise<User> {
|
|
|
|
|
|
try {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
const response = await authAPI.register(userData)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Use the common helper to set auth state
|
|
|
|
|
|
setAuthFromResponse(response)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
return user.value!
|
2025-12-18 13:50:39 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
// Clear any partial state on error
|
2025-12-25 08:41:43 -08:00
|
|
|
|
clearAuth()
|
|
|
|
|
|
throw error
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 12:05:25 +08:00
|
|
|
|
/**
|
2026-01-09 19:32:06 +08:00
|
|
|
|
* 直接设置 token(用于 OAuth/SSO 回调),并加载当前用户信息。
|
2026-02-05 12:38:48 +08:00
|
|
|
|
* 会自动读取 localStorage 中已设置的 refresh_token 和 token_expires_in
|
2026-01-09 19:32:06 +08:00
|
|
|
|
* @param newToken - 后端签发的 JWT access token
|
2026-01-09 12:05:25 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async function setToken(newToken: string): Promise<User> {
|
|
|
|
|
|
// Clear any previous state first (avoid mixing sessions)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Note: Don't clear localStorage here as OAuth callback may have set refresh_token
|
|
|
|
|
|
stopAutoRefresh()
|
|
|
|
|
|
stopTokenRefresh()
|
|
|
|
|
|
token.value = null
|
|
|
|
|
|
user.value = null
|
2026-01-09 12:05:25 +08:00
|
|
|
|
|
|
|
|
|
|
token.value = newToken
|
|
|
|
|
|
localStorage.setItem(AUTH_TOKEN_KEY, newToken)
|
|
|
|
|
|
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Read refresh token and expires_at from localStorage if set by OAuth callback
|
|
|
|
|
|
const savedRefreshToken = localStorage.getItem(REFRESH_TOKEN_KEY)
|
|
|
|
|
|
const savedExpiresAt = localStorage.getItem(TOKEN_EXPIRES_AT_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
if (savedRefreshToken) {
|
|
|
|
|
|
refreshTokenValue.value = savedRefreshToken
|
|
|
|
|
|
}
|
|
|
|
|
|
if (savedExpiresAt) {
|
|
|
|
|
|
tokenExpiresAt.value = parseInt(savedExpiresAt, 10)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 12:05:25 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const userData = await refreshUser()
|
|
|
|
|
|
startAutoRefresh()
|
2026-02-05 12:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
// Start proactive token refresh if we have refresh token and expiry info
|
|
|
|
|
|
// Note: use !== null to handle case when tokenExpiresAt.value is 0 (expired)
|
|
|
|
|
|
if (savedRefreshToken && tokenExpiresAt.value !== null) {
|
|
|
|
|
|
scheduleTokenRefreshAt(tokenExpiresAt.value)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-09 12:05:25 +08:00
|
|
|
|
return userData
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
clearAuth()
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* User logout
|
|
|
|
|
|
* Clears all authentication state and persisted data
|
|
|
|
|
|
*/
|
2026-02-05 12:38:48 +08:00
|
|
|
|
async function logout(): Promise<void> {
|
|
|
|
|
|
// Call API logout (revokes refresh token on server)
|
|
|
|
|
|
await authAPI.logout()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
// Clear state
|
2025-12-25 08:41:43 -08:00
|
|
|
|
clearAuth()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Refresh current user data
|
|
|
|
|
|
* Fetches latest user info from the server
|
|
|
|
|
|
* @returns Promise resolving to the updated user
|
|
|
|
|
|
* @throws Error if not authenticated or request fails
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function refreshUser(): Promise<User> {
|
|
|
|
|
|
if (!token.value) {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
throw new Error('Not authenticated')
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-12-29 03:17:25 +08:00
|
|
|
|
const response = await authAPI.getCurrentUser()
|
|
|
|
|
|
if (response.data.run_mode) {
|
|
|
|
|
|
runMode.value = response.data.run_mode
|
|
|
|
|
|
}
|
2026-01-05 13:54:43 +08:00
|
|
|
|
const { run_mode: _run_mode, ...userData } = response.data
|
2025-12-29 03:17:25 +08:00
|
|
|
|
user.value = userData
|
2025-12-25 08:41:43 -08:00
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
// Update localStorage
|
2025-12-29 03:17:25 +08:00
|
|
|
|
localStorage.setItem(AUTH_USER_KEY, JSON.stringify(userData))
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2025-12-29 03:17:25 +08:00
|
|
|
|
return userData
|
2025-12-18 13:50:39 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
// If refresh fails with 401, clear auth state
|
|
|
|
|
|
if ((error as { status?: number }).status === 401) {
|
2025-12-25 08:41:43 -08:00
|
|
|
|
clearAuth()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
2025-12-25 08:41:43 -08:00
|
|
|
|
throw error
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Clear all authentication state
|
|
|
|
|
|
* Internal helper function
|
|
|
|
|
|
*/
|
|
|
|
|
|
function clearAuth(): void {
|
|
|
|
|
|
// Stop auto-refresh
|
2025-12-25 08:41:43 -08:00
|
|
|
|
stopAutoRefresh()
|
2026-02-05 12:38:48 +08:00
|
|
|
|
// Stop token refresh
|
|
|
|
|
|
stopTokenRefresh()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
2025-12-25 08:41:43 -08:00
|
|
|
|
token.value = null
|
2026-02-05 12:38:48 +08:00
|
|
|
|
refreshTokenValue.value = null
|
|
|
|
|
|
tokenExpiresAt.value = null
|
2025-12-25 08:41:43 -08:00
|
|
|
|
user.value = null
|
|
|
|
|
|
localStorage.removeItem(AUTH_TOKEN_KEY)
|
|
|
|
|
|
localStorage.removeItem(AUTH_USER_KEY)
|
2026-02-05 12:38:48 +08:00
|
|
|
|
localStorage.removeItem(REFRESH_TOKEN_KEY)
|
|
|
|
|
|
localStorage.removeItem(TOKEN_EXPIRES_AT_KEY)
|
2025-12-18 13:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== Return Store API ====================
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
// State
|
|
|
|
|
|
user,
|
|
|
|
|
|
token,
|
2025-12-29 03:17:25 +08:00
|
|
|
|
runMode: readonly(runMode),
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
// Computed
|
|
|
|
|
|
isAuthenticated,
|
|
|
|
|
|
isAdmin,
|
2025-12-29 03:17:25 +08:00
|
|
|
|
isSimpleMode,
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
login,
|
2026-01-26 08:45:43 +08:00
|
|
|
|
login2FA,
|
2025-12-18 13:50:39 +08:00
|
|
|
|
register,
|
2026-01-09 12:05:25 +08:00
|
|
|
|
setToken,
|
2025-12-18 13:50:39 +08:00
|
|
|
|
logout,
|
|
|
|
|
|
checkAuth,
|
2025-12-25 08:41:43 -08:00
|
|
|
|
refreshUser
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|