26 lines
642 B
TypeScript
26 lines
642 B
TypeScript
import { ref } from 'vue'
|
|
|
|
// Token Service
|
|
//
|
|
// 設計重點:
|
|
// - 單一來源(Single Source of Truth):用 ref 維護 token,並同步 localStorage
|
|
// - Store 與 Interceptor 只透過此 service 讀寫 token,避免來源分裂
|
|
|
|
const storageKey = 'token'
|
|
const tokenRef = ref<string | null>(localStorage.getItem(storageKey))
|
|
|
|
export const tokenService = {
|
|
token: tokenRef,
|
|
getToken() {
|
|
return tokenRef.value
|
|
},
|
|
setToken(nextToken: string) {
|
|
tokenRef.value = nextToken
|
|
localStorage.setItem(storageKey, nextToken)
|
|
},
|
|
clearToken() {
|
|
tokenRef.value = null
|
|
localStorage.removeItem(storageKey)
|
|
},
|
|
}
|