Added /users/me route, and handling auth in frontend
This commit is contained in:
31
frontend/hooks/use-login.tsx
Normal file
31
frontend/hooks/use-login.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
"use client";
|
||||
|
||||
import { setCookie } from "cookies-next";
|
||||
import useApiMutation from "./use-api";
|
||||
|
||||
export interface LoginArgs {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export default function useLogin() {
|
||||
const {
|
||||
trigger,
|
||||
isMutating: loading,
|
||||
isSuccess,
|
||||
} = useApiMutation<string, LoginArgs>("/users/login", undefined, "POST");
|
||||
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user