File size: 1,097 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import type { AuthResponseDto, LoginDto } from "@reactive-resume/dto";
import { useMutation } from "@tanstack/react-query";
import type { AxiosResponse } from "axios";
import { useNavigate } from "react-router";
import { axios } from "@/client/libs/axios";
import { queryClient } from "@/client/libs/query-client";
import { useAuthStore } from "@/client/stores/auth";
export const login = async (data: LoginDto) => {
const response = await axios.post<AuthResponseDto, AxiosResponse<AuthResponseDto>, LoginDto>(
"/auth/login",
data,
);
return response.data;
};
export const useLogin = () => {
const navigate = useNavigate();
const setUser = useAuthStore((state) => state.setUser);
const {
error,
isPending: loading,
mutateAsync: loginFn,
} = useMutation({
mutationFn: login,
onSuccess: (data) => {
if (data.status === "2fa_required") {
void navigate("/auth/verify-otp");
return;
}
setUser(data.user);
queryClient.setQueryData(["user"], data.user);
},
});
return { login: loginFn, loading, error };
};
|