Skip to content

Commit e575881

Browse files
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>
1 parent 874835e commit e575881

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/utils/auth.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import { STORAGE_TOKEN_KEY } from '@/stores/mutation-type'
2+
import { useLocalStorage } from '@vueuse/core'
3+
4+
const token = useLocalStorage(STORAGE_TOKEN_KEY, '')
25

36
function isLogin() {
4-
return !!localStorage.getItem(STORAGE_TOKEN_KEY)
7+
return !!token.value
58
}
69

710
function getToken() {
8-
return localStorage.getItem(STORAGE_TOKEN_KEY)
11+
return token.value
912
}
1013

11-
function setToken(token: string) {
12-
localStorage.setItem(STORAGE_TOKEN_KEY, token)
14+
function setToken(newToken: string) {
15+
token.value = newToken
1316
}
1417

1518
function clearToken() {
16-
localStorage.removeItem(STORAGE_TOKEN_KEY)
19+
token.value = null
1720
}
1821

1922
export { isLogin, getToken, setToken, clearToken }

0 commit comments

Comments
 (0)