[common] feat: useSessionExtendConfirm 생성
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from "./cookie";
|
||||
export * from "./utils";
|
||||
export * from "./use-mobile";
|
||||
export * from "./use-session-extend-confirm";
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef } from "react";
|
||||
import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
||||
import { getRemainingMs, SESSION_EXTEND_CONFIRM_BUFFER_MS } from "@common/auth";
|
||||
|
||||
interface UseSessionExtendConfirmOptions {
|
||||
enabled: boolean;
|
||||
getAccessToken: () => string | undefined;
|
||||
silentRefresh: () => Promise<void>;
|
||||
declineSessionExtend: () => void;
|
||||
message?: string;
|
||||
bufferMs?: number;
|
||||
}
|
||||
|
||||
const DEFAULT_MESSAGE = "로그인을 연장하시겠습니까?";
|
||||
|
||||
function useSessionExtendConfirm({
|
||||
enabled,
|
||||
getAccessToken,
|
||||
silentRefresh,
|
||||
declineSessionExtend,
|
||||
message = DEFAULT_MESSAGE,
|
||||
}: UseSessionExtendConfirmOptions) {
|
||||
const { confirm } = useConfirm();
|
||||
const confirmTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const dismissedTokenRef = useRef<string | null>(null);
|
||||
const isConfirmingRef = useRef(false);
|
||||
|
||||
const scheduleConfirm = useCallback(() => {
|
||||
if (confirmTimerRef.current) {
|
||||
clearTimeout(confirmTimerRef.current);
|
||||
confirmTimerRef.current = null;
|
||||
}
|
||||
|
||||
const token = getAccessToken();
|
||||
if (!token) return;
|
||||
|
||||
if (dismissedTokenRef.current === token) return;
|
||||
|
||||
const confirmMs = getRemainingMs(token, SESSION_EXTEND_CONFIRM_BUFFER_MS);
|
||||
const isTokenValid = getRemainingMs(token, 0) > 0;
|
||||
|
||||
const showConfirm = async () => {
|
||||
if (isConfirmingRef.current) return;
|
||||
|
||||
const currentToken = getAccessToken();
|
||||
if (!currentToken || getRemainingMs(currentToken, 0) <= 0) return;
|
||||
if (dismissedTokenRef.current === currentToken) return;
|
||||
|
||||
isConfirmingRef.current = true;
|
||||
try {
|
||||
const accepted = await confirm(message);
|
||||
if (accepted) {
|
||||
dismissedTokenRef.current = null;
|
||||
await silentRefresh();
|
||||
scheduleConfirm();
|
||||
return;
|
||||
}
|
||||
|
||||
declineSessionExtend();
|
||||
dismissedTokenRef.current = currentToken;
|
||||
} finally {
|
||||
isConfirmingRef.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
if (!isTokenValid) return;
|
||||
|
||||
if (confirmMs <= 0) {
|
||||
void showConfirm();
|
||||
return;
|
||||
}
|
||||
|
||||
confirmTimerRef.current = setTimeout(() => {
|
||||
void showConfirm();
|
||||
}, confirmMs);
|
||||
}, [message, confirm, getAccessToken, silentRefresh, declineSessionExtend]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
|
||||
scheduleConfirm();
|
||||
|
||||
return () => {
|
||||
if (confirmTimerRef.current) {
|
||||
clearTimeout(confirmTimerRef.current);
|
||||
confirmTimerRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [enabled, scheduleConfirm]);
|
||||
}
|
||||
|
||||
export { useSessionExtendConfirm };
|
||||
export type { UseSessionExtendConfirmOptions };
|
||||
Reference in New Issue
Block a user