+5
-3
@@ -1,5 +1,5 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
|
||||
const TOKEN_KEY = 'oci-portal.token'
|
||||
const EXPIRES_KEY = 'oci-portal.expiresAt'
|
||||
@@ -8,11 +8,13 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref(localStorage.getItem(TOKEN_KEY) ?? '')
|
||||
const expiresAt = ref(localStorage.getItem(EXPIRES_KEY) ?? '')
|
||||
|
||||
const isAuthed = computed(() => {
|
||||
// 普通函数而非 computed:computed 只随 token/expiresAt 失效,时间流逝不会
|
||||
// 触发重算,令牌自然过期后路由守卫会长期误判仍已登录
|
||||
function isAuthed(): boolean {
|
||||
if (!token.value) return false
|
||||
if (!expiresAt.value) return true
|
||||
return new Date(expiresAt.value).getTime() > Date.now()
|
||||
})
|
||||
}
|
||||
|
||||
function setSession(newToken: string, newExpiresAt: string) {
|
||||
token.value = newToken
|
||||
|
||||
Reference in New Issue
Block a user