261030_01_FIX: 인증 전환 시 상태 정리 및 탭 동기화, 외부 링크 권한 오류 시 로그아웃 처리
This commit is contained in:
35
src/lib/auth-change.ts
Normal file
35
src/lib/auth-change.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { reportUrl } from "@/types/share";
|
||||
|
||||
// Set false to disable only cross-tab notifications and their listener.
|
||||
const AUTH_CHANGE_SYNC_ENABLED = true;
|
||||
const CHANNEL = "kart:auth-change";
|
||||
let channel: BroadcastChannel | undefined;
|
||||
|
||||
function authChannel() {
|
||||
if (!AUTH_CHANGE_SYNC_ENABLED || typeof BroadcastChannel === "undefined") return;
|
||||
try {
|
||||
return channel ??= new BroadcastChannel(CHANNEL);
|
||||
} catch {
|
||||
// Cross-tab sync is optional when the browser disallows messaging.
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function notifyAuthChange(path: string) {
|
||||
authChannel()?.postMessage(path);
|
||||
}
|
||||
|
||||
export function subscribeToAuthChanges() {
|
||||
const listener = authChannel();
|
||||
if (!listener) return;
|
||||
listener.onmessage = (event: MessageEvent<unknown>) => {
|
||||
const path = event.data === "/" || event.data === "/login"
|
||||
? event.data : reportUrl(event.data);
|
||||
// A full navigation discards this tab's cache/state without clearing the new cookie.
|
||||
if (path) window.location.replace(path);
|
||||
};
|
||||
return () => {
|
||||
listener.close();
|
||||
channel = undefined;
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createHttpClient } from "@kri/common/http";
|
||||
import { apiError, createHttpClient, redirectToLogout } from "@kri/common/http";
|
||||
import { clientEnv } from "@/lib/env.client";
|
||||
|
||||
const resolveBaseUrl = () =>
|
||||
@@ -7,6 +7,8 @@ const resolveBaseUrl = () =>
|
||||
: new URL(clientEnv.API_BASE, window.location.origin).toString();
|
||||
|
||||
export const nextApi = createHttpClient(resolveBaseUrl(), {
|
||||
onForbidden: false,
|
||||
onForbidden: (error) => {
|
||||
if (apiError(error).code === "EXTERNAL_SCOPE_DENIED") redirectToLogout();
|
||||
},
|
||||
onResponseError: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user