mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 05:54:08 +01:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import axios from "axios";
|
|
import axiosInstance from "./config";
|
|
|
|
export interface LoginResponse {
|
|
token: string;
|
|
message?: string;
|
|
}
|
|
|
|
export interface RegisterResponse {
|
|
token: string;
|
|
message?: string;
|
|
}
|
|
|
|
/**
|
|
* Registers a new user by sending a POST request to the backend.
|
|
*/
|
|
export async function registerUser(email: string, password: string): Promise<RegisterResponse> {
|
|
try {
|
|
const response = await axiosInstance.post("/auth/register", {
|
|
email: email,
|
|
password: password,
|
|
});
|
|
return response.data;
|
|
} catch (error: any) {
|
|
if (axios.isAxiosError(error)) {
|
|
throw new Error(error.response?.data?.message || "Failed to register.");
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Logs in a user by sending a POST request to the backend.
|
|
*/
|
|
export async function loginUser(email: string, password: string): Promise<LoginResponse> {
|
|
try {
|
|
const response = await axiosInstance.post("/auth/login", {
|
|
email: email,
|
|
password: password,
|
|
});
|
|
return response.data;
|
|
} catch (error: any) {
|
|
if (axios.isAxiosError(error)) {
|
|
throw new Error(error.response?.data?.message || "Failed to log in.");
|
|
}
|
|
throw error;
|
|
}
|
|
}
|