261030_01_FIX: 인증 전환 시 상태 정리 및 탭 동기화, 외부 링크 권한 오류 시 로그아웃 처리

This commit is contained in:
2026-10-30 15:21:34 +09:00
parent c6cc7edfc7
commit 4bfa2af213
6 changed files with 80 additions and 22 deletions

View File

@@ -1,17 +1,16 @@
"use client";
import type { LoginRequest } from "@kri/common";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useSetAtom } from "jotai";
import { useMutation } from "@tanstack/react-query";
import { useAuth } from "@/hooks/useAuth";
import { selectedProjectAtom } from "@/hooks/useSelectedProject";
import { useResetSession } from "@/hooks/useResetSession";
import { notifyAuthChange } from "@/lib/auth-change";
import { nextApi } from "@/lib/http-client";
import { isLoginUser } from "@/types/kart";
export function useLoginMutation() {
const { setFromLoginResponse } = useAuth();
const setSelectedProject = useSetAtom(selectedProjectAtom);
const queryClient = useQueryClient();
const resetSession = useResetSession();
return useMutation({
mutationFn: async (request: LoginRequest) => {
const user = await nextApi.post<unknown>("/login", request, {
@@ -22,18 +21,17 @@ export function useLoginMutation() {
if (!isLoginUser(user)) throw new Error("로그인 응답을 확인해 주세요.");
return user;
},
onSuccess: (user) => {
queryClient.clear();
onSuccess: async (user) => {
// Legacy Login.js starts every session without a selected project.
setSelectedProject(null);
await resetSession();
setFromLoginResponse(user);
notifyAuthChange("/");
},
});
}
export function useLogoutMutation() {
const queryClient = useQueryClient();
const { logout } = useAuth();
const setSelectedProject = useSetAtom(selectedProjectAtom);
const resetSession = useResetSession();
return useMutation({
mutationFn: () =>
nextApi.post("/logout", null, {
@@ -41,10 +39,10 @@ export function useLogoutMutation() {
skipForbiddenHandler: true,
skipResponseErrorHandler: true,
}),
onSettled: () => {
onSettled: async () => {
await resetSession();
logout();
setSelectedProject(null);
queryClient.clear();
notifyAuthChange("/login");
},
});
}