feat: add token validation api and auth module

This commit is contained in:
Tim
2025-07-05 13:34:47 +08:00
parent 3568e54984
commit 6bdc32ff05
4 changed files with 42 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
import { API_BASE_URL } from '../main'
const TOKEN_KEY = 'token'
export function getToken() {
return localStorage.getItem(TOKEN_KEY)
}
export function setToken(token) {
localStorage.setItem(TOKEN_KEY, token)
}
export function clearToken() {
localStorage.removeItem(TOKEN_KEY)
}
export async function checkToken() {
const token = getToken()
if (!token) return false
try {
const res = await fetch(`${API_BASE_URL}/api/auth/check`, {
headers: { Authorization: `Bearer ${token}` }
})
return res.ok
} catch (e) {
return false
}
}