42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { nextApi } from "@/lib/http-client";
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import type { LoginUser } from "@/types/auth";
|
|
|
|
export const useLoginMutation = () => {
|
|
const { setFromLoginResponse } = useAuth();
|
|
|
|
return useMutation({
|
|
mutationFn: async (v: { userId: string; userPw: string;}) => {
|
|
return nextApi.post<LoginUser>("/login", { userId: v.userId, userPw: v.userPw }, { autoLogoutOnAuthError: false });
|
|
},
|
|
onSuccess: (data, v) => {
|
|
setFromLoginResponse(data);
|
|
},
|
|
});
|
|
}
|
|
|
|
export const useLogoutMutation = () => {
|
|
const qc = useQueryClient();
|
|
const { auth, logout } = useAuth();
|
|
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
const userId = auth.user?.userId ?? "";
|
|
try {
|
|
if (userId){
|
|
await nextApi.post(`/logout/${userId}`, null, { autoLogoutOnAuthError: false, suppressAlert: true });
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
},
|
|
onSettled: () => {
|
|
logout();
|
|
qc.clear(); // 다음 사용자/세션에 서버상태 남지 않게
|
|
},
|
|
});
|
|
}
|