feat(static): 首页添加登录态检测与进入笔记入口
- index.html: lang 改为 zh-CN,新增"关于本站"介绍段落,按钮改为有 id 的登录/注册/进入/退出四按钮(默认隐藏进入/退出) - 新增 index.js: POST /whoami 检测登录态(HttpOnly Cookie 自动携带),已登录则显示用户名+进入笔记/退出按钮,未登录显示登录/注册 - 退出按钮调用 POST /logout 后刷新页面
This commit is contained in:
1 parent
84adec4722
commit
1a5e9f7064
2 files changed
+61
-5
No files matched your search
@@ -0,0 +1,42 @@
|
||||
/* index.js — 社区首页逻辑
|
||||
*
|
||||
* 登录态检测:POST /whoami(HttpOnly q4_token Cookie 自动携带,JS 不经手 Token)
|
||||
* 已登录:显示用户名与"进入笔记/退出"按钮;未登录:显示"登录/注册"
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
const nameEl = document.getElementById('name');
|
||||
const enterBtn = document.getElementById('enterBtn');
|
||||
const logoutBtn = document.getElementById('logoutBtn');
|
||||
const loginBtn = document.getElementById('loginBtn');
|
||||
const registerBtn = document.getElementById('registerBtn');
|
||||
|
||||
async function checkLogin() {
|
||||
try {
|
||||
const res = await fetch('/whoami', { method: 'POST' });
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
if (data && data.login && data.user) {
|
||||
nameEl.textContent = String(data.user);
|
||||
enterBtn.style.display = '';
|
||||
logoutBtn.style.display = '';
|
||||
loginBtn.style.display = 'none';
|
||||
registerBtn.style.display = 'none';
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('index: 登录态检测失败', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
logoutBtn.addEventListener('click', async function () {
|
||||
try {
|
||||
await fetch('/logout', { method: 'POST' });
|
||||
} catch (e) {
|
||||
console.warn('index: 退出登录请求异常', e.message);
|
||||
}
|
||||
location.reload();
|
||||
});
|
||||
|
||||
checkLogin();
|
||||
})();
|
||||
Reference in new issue
Block a user