261030_01_FIX: 인증 전환 시 상태 정리 및 탭 동기화, 외부 링크 권한 오류 시 로그아웃 처리
This commit is contained in:
@@ -4,9 +4,10 @@ import { useEffect, useRef, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import type { Route } from "next";
|
||||
import { useSetAtom } from "jotai";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { apiError } from "@kri/common/http";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { useResetSession } from "@/hooks/useResetSession";
|
||||
import { notifyAuthChange } from "@/lib/auth-change";
|
||||
import { selectedProjectAtom } from "@/hooks/useSelectedProject";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import { isExternalSession, reportUrl, type ExternalSession } from "@/types/share";
|
||||
@@ -16,7 +17,7 @@ export function ExternalEntry({ urlKey }: { urlKey: string }) {
|
||||
const router = useRouter();
|
||||
const { hydrated, setAuth, logout } = useAuth();
|
||||
const setProject = useSetAtom(selectedProjectAtom);
|
||||
const queryClient = useQueryClient();
|
||||
const resetSession = useResetSession();
|
||||
const pending = useRef<Promise<ExternalSession> | null>(null);
|
||||
const [error, setError] = useState("");
|
||||
useEffect(() => {
|
||||
@@ -24,9 +25,7 @@ export function ExternalEntry({ urlKey }: { urlKey: string }) {
|
||||
let active = true;
|
||||
const enter = async () => {
|
||||
logout();
|
||||
setProject(null);
|
||||
await queryClient.cancelQueries();
|
||||
queryClient.clear();
|
||||
await resetSession();
|
||||
const result = await nextApi.post<unknown>(`/external/${encodeURIComponent(urlKey)}/login`, null, {
|
||||
skipUnauthorizedHandler: true, skipForbiddenHandler: true, skipResponseErrorHandler: true,
|
||||
});
|
||||
@@ -39,12 +38,16 @@ export function ExternalEntry({ urlKey }: { urlKey: string }) {
|
||||
if (!active) return;
|
||||
setProject({ userId: result.user.userId, project: { projectType: result.link.projectType, pkey: result.link.pkey } });
|
||||
setAuth({ user: result.user, external: result.link });
|
||||
notifyAuthChange(result.link.realUrl);
|
||||
router.replace(reportUrl(result.link.realUrl) as Route);
|
||||
}).catch((reason: unknown) => {
|
||||
if (active) setError(apiError(reason, { fallbackMessage: "공유 링크를 확인해 주세요." }).message);
|
||||
if (active) {
|
||||
notifyAuthChange("/login");
|
||||
setError(apiError(reason, { fallbackMessage: "공유 링크를 확인해 주세요." }).message);
|
||||
}
|
||||
});
|
||||
return () => { active = false; };
|
||||
}, [hydrated, logout, setAuth, setProject, queryClient, router, urlKey]);
|
||||
}, [hydrated, logout, setAuth, setProject, resetSession, router, urlKey]);
|
||||
if (error) return <p role="alert" className="p-8 text-center text-sm">{error}</p>;
|
||||
return <LoadingOverlayAdapter force label="공유 링크를 확인하는 중입니다." />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user