events api file init

This commit is contained in:
gom-by
2025-01-27 10:15:05 +01:00
parent 78ce2b533b
commit c202764966
6 changed files with 226 additions and 40 deletions

38
frontend/hooks/events.tsx Normal file
View File

@@ -0,0 +1,38 @@
"use client";
import { setCookie } from "cookies-next";
import { useEffect, useState } from "react";
import { API_URL } from "@/lib/constants";
export interface LoginArgs {
email: string;
password: string;
}
export default function useLogin() {
const {
data,
isLoading: loading,
isSuccess,
} = request<string, LoginArgs>(
"/users/login",
undefined,
"POST",
false,
true,
);
const login = async (inputs: LoginArgs) => {
try {
const res = await trigger(inputs);
if (!res) throw new Error("The server hasn't responded.");
if (res.status === "Error") throw new Error(res.message);
if (res.data) setCookie("auth_token", res.data);
return res;
} catch (error: any) {
throw new Error(error.message);
}
};
return { login, loading, isSuccess };
}