refactor(auth): use vueuse's useLocalStorage (#142)

* refactor(auth): replace native localStorage API with vueuse's useLocalStorage API

* chore(auth): Add explicit import for useLocalStorage

* chore(auth): Set token to null

---------

Co-authored-by: Charlie  <18888351756@163.com>
This commit is contained in:
陈升 2025-01-02 17:17:17 +08:00 committed by GitHub
parent 874835e32b
commit e575881c28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 5 deletions

View File

@ -1,19 +1,22 @@
import { STORAGE_TOKEN_KEY } from '@/stores/mutation-type'
import { useLocalStorage } from '@vueuse/core'
const token = useLocalStorage(STORAGE_TOKEN_KEY, '')
function isLogin() {
return !!localStorage.getItem(STORAGE_TOKEN_KEY)
return !!token.value
}
function getToken() {
return localStorage.getItem(STORAGE_TOKEN_KEY)
return token.value
}
function setToken(token: string) {
localStorage.setItem(STORAGE_TOKEN_KEY, token)
function setToken(newToken: string) {
token.value = newToken
}
function clearToken() {
localStorage.removeItem(STORAGE_TOKEN_KEY)
token.value = null
}
export { isLogin, getToken, setToken, clearToken }