101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import { requestJson, setCsrfToken } from "./http";
|
|
import type { AuthSession, AuthUser } from "./types";
|
|
|
|
interface AuthResponse {
|
|
user: AuthUser;
|
|
csrfToken: string | null;
|
|
}
|
|
|
|
function applyAuthResponse(response: AuthResponse): AuthResponse {
|
|
setCsrfToken(response.csrfToken);
|
|
return response;
|
|
}
|
|
|
|
export async function getCurrentUser(): Promise<AuthResponse> {
|
|
return applyAuthResponse(await requestJson<AuthResponse>("/auth/me"));
|
|
}
|
|
|
|
export async function register(payload: {
|
|
email: string;
|
|
password: string;
|
|
turnstileToken: string;
|
|
}): Promise<void> {
|
|
await requestJson<void>("/auth/register", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function login(payload: { email: string; password: string }): Promise<AuthResponse> {
|
|
return applyAuthResponse(
|
|
await requestJson<AuthResponse>("/auth/login", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
}),
|
|
);
|
|
}
|
|
|
|
export async function logout(): Promise<void> {
|
|
await requestJson<void>("/auth/logout", { method: "POST" });
|
|
setCsrfToken(null);
|
|
}
|
|
|
|
export async function verifyEmail(token: string): Promise<void> {
|
|
await requestJson<void>("/auth/verify-email", {
|
|
method: "POST",
|
|
body: JSON.stringify({ token }),
|
|
});
|
|
}
|
|
|
|
export async function resendVerification(email: string): Promise<void> {
|
|
await requestJson<void>("/auth/resend-verification", {
|
|
method: "POST",
|
|
body: JSON.stringify({ email }),
|
|
});
|
|
}
|
|
|
|
export async function forgotPassword(email: string): Promise<void> {
|
|
await requestJson<void>("/auth/forgot-password", {
|
|
method: "POST",
|
|
body: JSON.stringify({ email }),
|
|
});
|
|
}
|
|
|
|
export async function resetPassword(token: string, password: string): Promise<void> {
|
|
await requestJson<void>("/auth/reset-password", {
|
|
method: "POST",
|
|
body: JSON.stringify({ token, password }),
|
|
});
|
|
}
|
|
|
|
export async function getSessions(): Promise<AuthSession[]> {
|
|
const response = await requestJson<{ sessions: AuthSession[] }>("/auth/sessions");
|
|
return response.sessions;
|
|
}
|
|
|
|
export async function changePassword(payload: { currentPassword: string; newPassword: string }): Promise<void> {
|
|
await requestJson<void>("/auth/password", {
|
|
method: "POST",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function revokeSession(sessionId: string): Promise<void> {
|
|
await requestJson<void>(`/auth/sessions/${sessionId}`, { method: "DELETE" });
|
|
}
|
|
|
|
export async function revokeOtherSessions(): Promise<void> {
|
|
await requestJson<void>("/auth/sessions/revoke-others", { method: "POST" });
|
|
}
|
|
|
|
export async function updateProfile(payload: {
|
|
profileUsername?: string | null;
|
|
isFutureRacesPublic?: boolean;
|
|
isCompletedRacesPublic?: boolean;
|
|
}): Promise<void> {
|
|
await requestJson<void>("/auth/profile", {
|
|
method: "PATCH",
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|