mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 14:40:49 +08:00
feat(ui): make auth state reactive and close avatar menu
This commit is contained in:
@@ -1,17 +1,26 @@
|
||||
import { API_BASE_URL } from '../main'
|
||||
import { reactive } from 'vue'
|
||||
|
||||
const TOKEN_KEY = 'token'
|
||||
|
||||
export const authState = reactive({
|
||||
loggedIn: false
|
||||
})
|
||||
|
||||
authState.loggedIn = localStorage.getItem(TOKEN_KEY) !== null && localStorage.getItem(TOKEN_KEY) !== ''
|
||||
|
||||
export function getToken() {
|
||||
return localStorage.getItem(TOKEN_KEY)
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
localStorage.setItem(TOKEN_KEY, token)
|
||||
authState.loggedIn = true
|
||||
}
|
||||
|
||||
export function clearToken() {
|
||||
localStorage.removeItem(TOKEN_KEY)
|
||||
authState.loggedIn = false
|
||||
}
|
||||
|
||||
export async function fetchCurrentUser() {
|
||||
@@ -29,9 +38,7 @@ export async function fetchCurrentUser() {
|
||||
}
|
||||
|
||||
export function isLogin() {
|
||||
const token = getToken()
|
||||
console.log('token', token)
|
||||
return token !== null && token !== ''
|
||||
return authState.loggedIn
|
||||
}
|
||||
|
||||
export async function checkToken() {
|
||||
@@ -41,8 +48,10 @@ export async function checkToken() {
|
||||
const res = await fetch(`${API_BASE_URL}/api/auth/check`, {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
authState.loggedIn = res.ok
|
||||
return res.ok
|
||||
} catch (e) {
|
||||
authState.loggedIn = false
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user