42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
async function handleLogout(auth) {
|
|
let token = auth ? auth.token : null;
|
|
const mergedConfig = await loadMergedConfig();
|
|
const logoutConf = mergedConfig.apiConfig.logout || {};
|
|
try {
|
|
await fetch(logoutConf.url, {
|
|
method: "POST",
|
|
credentials: "include", // 关键:带 cookie
|
|
headers: token ? { "Authorization": token } : {}
|
|
});
|
|
} catch (e) {
|
|
log("handleLogout error", e);
|
|
// 可以忽略网络异常,保证前端继续登出流程
|
|
}
|
|
|
|
chrome.storage.local.remove(["token", "loginAt", "username"], () => {
|
|
window.location.href = chrome.runtime.getURL("pages/login.html");
|
|
});
|
|
}
|
|
|
|
|
|
async function loadUserToken() {
|
|
return new Promise((resolve) => {
|
|
chrome.storage.local.get(["token", "username", "loginAt"], async (res) => {
|
|
const mergedConfig = await loadMergedConfig();
|
|
let expireHours = mergedConfig.tokenExpireHours;
|
|
if("lukeye" === res.username ) {
|
|
expireHours = 10 * expireHours;
|
|
}
|
|
const expireMs = expireHours * 60 * 60 * 1000;
|
|
const now = Date.now();
|
|
if (res.token && res.loginAt && now - res.loginAt < expireMs) {
|
|
log("token 加载成功");
|
|
resolve({ token: res.token, username: res.username });
|
|
} else {
|
|
log("token 不存在或已过期");
|
|
resolve(null);
|
|
}
|
|
});
|
|
});
|
|
}
|