Files
nysoure/frontend/src/app.ts
2025-06-04 10:20:01 +08:00

58 lines
1.2 KiB
TypeScript

import { User } from "./network/models.ts";
interface MyWindow extends Window {
serverName?: string;
cloudflareTurnstileSiteKey?: string;
siteInfo?: string;
}
class App {
appName = "Nysoure";
user: User | null = null;
token: string | null = null;
cloudflareTurnstileSiteKey: string | null = null;
siteInfo = "";
constructor() {
this.init();
}
init() {
const userJson = localStorage.getItem("user");
const tokenJson = localStorage.getItem("token");
if (userJson) {
this.user = JSON.parse(userJson);
}
if (tokenJson) {
this.token = JSON.parse(tokenJson);
}
this.appName = (window as MyWindow).serverName || this.appName;
this.cloudflareTurnstileSiteKey =
(window as MyWindow).cloudflareTurnstileSiteKey || null;
this.siteInfo = (window as MyWindow).siteInfo || "";
}
saveData() {
localStorage.setItem("user", JSON.stringify(this.user));
localStorage.setItem("token", JSON.stringify(this.token));
}
isAdmin() {
return this.user != null && this.user.is_admin;
}
isLoggedIn() {
return this.user != null && this.token != null;
}
canUpload() {
return this.isLoggedIn() && (this.user?.can_upload || this.isAdmin());
}
}
export const app = new App();