Spaces:
Paused
Paused
File size: 2,115 Bytes
4e7644b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import { getApiUrl } from "../config";
import { responseErrorMessage } from "../error";
export interface LoginResponse {
token: string;
username: string;
message?: string;
}
export interface AuthStatusResponse {
enabled: boolean;
has_users: boolean;
mode?: "hub";
bootstrap_required?: boolean;
registration_enabled?: boolean;
}
export const authApi = {
login: async (username: string, password: string): Promise<LoginResponse> => {
const res = await fetch(getApiUrl("/auth/login"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
if (!res.ok) {
throw new Error(await responseErrorMessage(res, "Login failed"));
}
return res.json();
},
register: async (
username: string,
password: string,
): Promise<LoginResponse> => {
const res = await fetch(getApiUrl("/auth/register"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
if (!res.ok) {
throw new Error(await responseErrorMessage(res, "Registration failed"));
}
return res.json();
},
getStatus: async (): Promise<AuthStatusResponse> => {
const res = await fetch(getApiUrl("/auth/status"));
if (!res.ok) throw new Error("Failed to check auth status");
return res.json();
},
updateProfile: async (
currentPassword: string,
newUsername?: string,
newPassword?: string,
): Promise<LoginResponse> => {
const token = localStorage.getItem("qwenpaw_auth_token") || "";
const res = await fetch(getApiUrl("/auth/update-profile"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
current_password: currentPassword,
new_username: newUsername || null,
new_password: newPassword || null,
}),
});
if (!res.ok) {
throw new Error(await responseErrorMessage(res, "Update failed"));
}
return res.json();
},
};
|