Files
MC_Report/public/js/auth.js
canglan 8c5ce78fd0 chore: add AGPLv3 copyright header to all source files
- 52 JS files (backend + public/js): header with
  Copyright (C) 2026 Sea Network Technology Studio
  Author: CangLan <admin@sea-studio.top>
  + AGPLv3 notice
- idempotent (skips if header present), all syntax-checked
2026-08-19 20:27:05 +08:00

50 lines
1.7 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';
},
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'); },
};