feat: add registration and authentication
This commit is contained in:
69
frontend/src/api/auth.ts
Normal file
69
frontend/src/api/auth.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { requestJson, setCsrfToken } from "./http";
|
||||
import type { 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 }),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user