Files
2026-06-27 10:59:59 +08:00

36 lines
1.0 KiB
JavaScript

const AUTH = {
async me() {
const res = await fetch("/api/auth/me", { credentials: "include" });
if (!res.ok) return null;
return res.json();
},
async login(username, password) {
const res = await fetch("/api/auth/login", {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.detail || "登录失败");
return data;
},
async logout() {
await fetch("/api/auth/logout", { method: "POST", credentials: "include" });
},
async changeCredentials(payload) {
const res = await fetch("/api/auth/change-credentials", {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.detail || "修改失败");
return data;
},
};