[common] feat: useSessionExtendConfirm 생성

This commit is contained in:
2026-07-08 15:43:42 +09:00
parent 28d54b615b
commit eb84a23d7e
12 changed files with 478 additions and 45 deletions
+9 -6
View File
@@ -200,17 +200,20 @@ if (expiry) {
}
```
#### `getRemainingMs(token: string): number`
#### `getRemainingMs(token: string, bufferMs?: number): number`
토큰 만료 1분 전까지 남은 시간(밀리초)을 반환합니다.
토큰 만료 `bufferMs` 전까지 남은 시간(밀리초)을 반환합니다. 기본 `bufferMs`는 `REFRESH_BUFFER_MS`(1분)입니다.
```typescript
import { getRemainingMs } from "@common/auth";
import {
getRemainingMs,
REFRESH_BUFFER_MS,
SESSION_EXTEND_CONFIRM_BUFFER_MS,
} from "@common/auth";
const remaining = getRemainingMs(token);
if (remaining > 0) {
console.log("Token valid for:", remaining, "ms");
}
const confirmIn = getRemainingMs(token, SESSION_EXTEND_CONFIRM_BUFFER_MS);
const isValid = getRemainingMs(token, 0) > 0;
```
### 쿠키 유틸리티
+6 -1
View File
@@ -2,7 +2,12 @@ export { createTokenManager } from "./token-manager";
export type { TokenManager } from "./token-manager";
export { getCookie, setCookie, removeCookie } from "./cookie";
export { getJwtExpiry, getRemainingMs } from "./jwt-utils";
export {
getJwtExpiry,
getRemainingMs,
REFRESH_BUFFER_MS,
SESSION_EXTEND_CONFIRM_BUFFER_MS,
} from "./jwt-utils";
export type {
AuthConfig,
+10 -4
View File
@@ -19,14 +19,20 @@ export function getJwtExpiry(token: string): number | null {
}
}
export const REFRESH_BUFFER_MS = 60_000;
export const SESSION_EXTEND_CONFIRM_BUFFER_MS = 5 * 60_000;
/**
* 토큰 만료 1분 전까지 남은 시간(ms)을 반환합니다.
* 이미 만료됐거나 파싱 실패 시 0을 반환합니다.
* 토큰 만료 bufferMs 전까지 남은 시간(ms)을 반환합니다.
* 파싱 실패 시 0. bufferMs 기본값은 silent refresh용 1분.
*/
export function getRemainingMs(token: string): number {
export function getRemainingMs(
token: string,
bufferMs: number = REFRESH_BUFFER_MS,
): number {
const exp = getJwtExpiry(token);
if (!exp) return 0;
const remainingMs = exp * 1000 - Date.now() - 60_000;
const remainingMs = exp * 1000 - Date.now() - bufferMs;
return Math.max(0, remainingMs);
}
+5 -1
View File
@@ -47,11 +47,15 @@ export function createTokenManager(config: AuthConfig) {
}, delayMs);
},
clearAuthToken(): void {
clearRefreshTimer(): void {
if (refreshTimer) {
clearTimeout(refreshTimer);
refreshTimer = null;
}
},
clearAuthToken(): void {
this.clearRefreshTimer();
this.removeToken(config.claimName);
this.removeToken(config.authUserId);
+1
View File
@@ -6,6 +6,7 @@ export interface AuthPayload {
export interface AuthService {
login(payload: AuthPayload): Promise<void>;
silentRefresh(): Promise<void>;
declineSessionExtend(): void;
logout(): Promise<void>;
}