Files
beenvoice-app/lib/auth-api.ts
T

47 lines
1.3 KiB
TypeScript

import { getApiUrl } from "@/lib/config";
import { readHttpErrorMessage } from "@/lib/trpc-errors";
export async function registerAccount(input: {
firstName: string;
lastName: string;
email: string;
password: string;
}) {
const res = await fetch(`${getApiUrl()}/api/auth/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(input),
});
if (!res.ok) {
throw new Error(await readHttpErrorMessage(res));
}
}
export async function requestPasswordReset(email: string) {
const res = await fetch(`${getApiUrl()}/api/auth/forgot-password`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
if (!res.ok) {
throw new Error(await readHttpErrorMessage(res));
}
const data = (await res.json()) as { message?: string };
return data.message ?? "Check your email for reset instructions.";
}
export async function resetPassword(token: string, password: string) {
const res = await fetch(`${getApiUrl()}/api/auth/reset-password`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token, password }),
});
if (!res.ok) {
throw new Error(await readHttpErrorMessage(res));
}
}