24 lines
818 B
JavaScript
24 lines
818 B
JavaScript
// scripts/auth.js
|
|
const TOKEN_EXPIRE_MS = 7 * 24 * 60 * 60 * 1000; // 7天
|
|
|
|
async function loadToken() {
|
|
return new Promise((resolve) => {
|
|
chrome.storage.local.get(["token", "loginAt", "username"], (res) => {
|
|
const now = Date.now();
|
|
if (!res.token || !res.loginAt || isNaN(res.loginAt) || (now - res.loginAt > TOKEN_EXPIRE_MS)) {
|
|
log("token 不存在或已过期");
|
|
resolve(null);
|
|
} else {
|
|
log("token 加载成功");
|
|
resolve({ token: res.token, username: res.username || "用户" });
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function handleLogout() {
|
|
chrome.storage.local.remove(["token", "loginAt", "username"], () => {
|
|
window.location.href = chrome.runtime.getURL("pages/login.html");
|
|
});
|
|
}
|