260615_01_FST: First Commit
This commit is contained in:
41
src/query/auth.ts
Normal file
41
src/query/auth.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
"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 });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
logout();
|
||||
qc.clear(); // 다음 사용자/세션에 서버상태 남지 않게
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user