初始提交:OCI 面板前端(含 GenAI 网关一期)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
const TOKEN_KEY = 'oci-portal.token'
|
||||
const EXPIRES_KEY = 'oci-portal.expiresAt'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref(localStorage.getItem(TOKEN_KEY) ?? '')
|
||||
const expiresAt = ref(localStorage.getItem(EXPIRES_KEY) ?? '')
|
||||
|
||||
const isAuthed = computed(() => {
|
||||
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
|
||||
expiresAt.value = newExpiresAt
|
||||
localStorage.setItem(TOKEN_KEY, newToken)
|
||||
localStorage.setItem(EXPIRES_KEY, newExpiresAt)
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = ''
|
||||
expiresAt.value = ''
|
||||
localStorage.removeItem(TOKEN_KEY)
|
||||
localStorage.removeItem(EXPIRES_KEY)
|
||||
}
|
||||
|
||||
return { token, expiresAt, isAuthed, setSession, logout }
|
||||
})
|
||||
Reference in New Issue
Block a user