- app.js init(): use native fetch (no Bearer) to call /api/install/status and /api/settings/settings, so site_name loads even before login state is confirmed; call Auth.reset() if logged but no site_name set - auth.js: add reset() method that clears TK/US without redirecting - app.js route(): remove early return for uninstalled; let install check run first so init() can fall through to normal routing when installed - app.js renderMain(): catch 401/login errors from page render, clear Auth state, show login prompt with button that redirects back after login (target hash preserved)
56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
/*
|
|
* MC Report System
|
|
* Copyright (C) 2026 Sea Network Technology Studio
|
|
* Author: CangLan <admin@sea-studio.top>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
const Auth = {
|
|
TK: 'mc_token',
|
|
US: 'mc_user',
|
|
|
|
token() { return localStorage.getItem(this.TK); },
|
|
user() { try { return JSON.parse(localStorage.getItem(this.US)); } catch { return null; } },
|
|
logged() { return !!(this.token() && this.user()); },
|
|
|
|
async login(username, password) {
|
|
const d = await API.post('/auth/login', { username, password });
|
|
localStorage.setItem(this.TK, d.token);
|
|
localStorage.setItem(this.US, JSON.stringify(d.user));
|
|
return d.user;
|
|
},
|
|
|
|
async register(data) {
|
|
return API.post('/auth/register', data);
|
|
},
|
|
|
|
logout() {
|
|
localStorage.removeItem(this.TK);
|
|
localStorage.removeItem(this.US);
|
|
location.hash = '#/login';
|
|
},
|
|
|
|
reset() {
|
|
// 清除本地登录态但不清除 URL(调用方决定是否跳转)
|
|
localStorage.removeItem(this.TK);
|
|
localStorage.removeItem(this.US);
|
|
},
|
|
|
|
role(...r) { const u = this.user(); return u && r.includes(u.role); },
|
|
isStaff() { return this.role('owner', 'admin'); },
|
|
isAdmin() { return this.role('owner', 'admin'); },
|
|
isOwner() { return this.role('owner'); },
|
|
};
|