39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { useCallback } from "react";
|
|
|
|
interface User {
|
|
id: string | null | undefined;
|
|
email: string | null | undefined;
|
|
name: string | null | undefined;
|
|
}
|
|
|
|
interface LoginCredentials {
|
|
email: string;
|
|
password: string;
|
|
}
|
|
|
|
interface RegisterCredentials extends LoginCredentials {
|
|
name?: string;
|
|
}
|
|
|
|
interface AuthResponse {
|
|
user: User;
|
|
message?: string;
|
|
error?: string;
|
|
}
|
|
|
|
export const register = async (credentials: RegisterCredentials): Promise<AuthResponse> => {
|
|
const response = await fetch("/api/auth/register", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(credentials), // turns credentials obj into json
|
|
credentials: "include"
|
|
});
|
|
|
|
const data: AuthResponse = await response.json(); // gets response data from promise and turns response into object
|
|
|
|
if (!response.ok) { // boolean value depending on success code
|
|
throw new Error(data.error || "Registration failed");
|
|
}
|
|
|
|
return data
|
|
} |