55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
const AUTH_TOKEN_KEY = 'token';
|
|
|
|
/**
|
|
* 获取当前登录 token。
|
|
*/
|
|
export async function getToken(): Promise<string | null> {
|
|
const storage = getChromeStorage();
|
|
|
|
if (storage) {
|
|
const result = await storage.get(AUTH_TOKEN_KEY);
|
|
const value = result[AUTH_TOKEN_KEY];
|
|
return typeof value === 'string' && value.length > 0 ? value : null;
|
|
}
|
|
|
|
return window.localStorage.getItem(AUTH_TOKEN_KEY);
|
|
}
|
|
|
|
|
|
/**
|
|
* 清除当前登录 token。
|
|
*/
|
|
export async function logout(): Promise<void> {
|
|
const storage = getChromeStorage();
|
|
|
|
if (storage) {
|
|
await storage.remove(AUTH_TOKEN_KEY);
|
|
return;
|
|
}
|
|
console.log("溢出")
|
|
|
|
window.localStorage.removeItem(AUTH_TOKEN_KEY);
|
|
}
|
|
|
|
export async function setToken(token: string): Promise<void> {
|
|
const storage = getChromeStorage();
|
|
|
|
if (storage) {
|
|
await storage.set({ [AUTH_TOKEN_KEY]: token });
|
|
return;
|
|
}
|
|
|
|
window.localStorage.setItem(AUTH_TOKEN_KEY, token);
|
|
}
|
|
|
|
/**
|
|
* * 获取谷歌缓存
|
|
*/
|
|
function getChromeStorage(): chrome.storage.StorageArea | null {
|
|
if (typeof chrome === 'undefined' || !chrome.storage?.local) {
|
|
return null;
|
|
}
|
|
|
|
return chrome.storage.local;
|
|
}
|