@thinkingcat/auth-utils 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/middleware/handler.js +41 -0
- package/package.json +1 -1
|
@@ -72,6 +72,47 @@ async function handleMiddleware(req, config, options) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
const effectiveRole = (0, roles_js_1.getEffectiveRole)(token, serviceId);
|
|
75
|
+
// Relaxed Cookie Cleanup - 어떠한 인증 토큰도 없을 때만 잔여 쿠키 정리
|
|
76
|
+
const refreshTokenCookieName = `${cookiePrefix}_refresh_token`;
|
|
77
|
+
const refreshTokenCookie = req.cookies.get(refreshTokenCookieName);
|
|
78
|
+
// 공개 경로 및 루트(/)의 토큰 처리 경로는 제외
|
|
79
|
+
const isPublicPath = config.publicPaths.some((path) => pathname === path || pathname.startsWith(path));
|
|
80
|
+
const isAuthFlow = pathname === '/' && req.nextUrl.searchParams.has('token');
|
|
81
|
+
if (!isPublicPath && !isAuthFlow && !token && !refreshTokenCookie) {
|
|
82
|
+
// 인증 관련 쿠키 패턴
|
|
83
|
+
const authCookiePatterns = [
|
|
84
|
+
/^next-auth\./,
|
|
85
|
+
/^__Secure-next-auth\./,
|
|
86
|
+
/_access_token$/,
|
|
87
|
+
/_refresh_token$/,
|
|
88
|
+
/^auth-token$/,
|
|
89
|
+
/^__Secure-auth-token$/,
|
|
90
|
+
];
|
|
91
|
+
const hasAuthCookies = req.cookies.getAll().some((cookie) => authCookiePatterns.some((pattern) => pattern.test(cookie.name)));
|
|
92
|
+
if (hasAuthCookies) {
|
|
93
|
+
(0, logger_js_1.debugLog)('handleMiddleware', `Inconsistent state detected (no session, no refresh token, but auth cookies exist). Cleaning up.`);
|
|
94
|
+
const ssoBaseURL = options.ssoBaseURL;
|
|
95
|
+
const response = await (0, redirect_js_1.redirectToSSOLogin)(req, serviceId, ssoBaseURL);
|
|
96
|
+
// 모든 쿠키 확인하고 인증 관련 쿠키 삭제
|
|
97
|
+
req.cookies.getAll().forEach((cookie) => {
|
|
98
|
+
const isAuthCookie = authCookiePatterns.some((pattern) => pattern.test(cookie.name));
|
|
99
|
+
if (isAuthCookie) {
|
|
100
|
+
(0, logger_js_1.debugLog)('handleMiddleware', `Deleting stale cookie: ${cookie.name}`);
|
|
101
|
+
response.cookies.delete(cookie.name);
|
|
102
|
+
// 추가로 만료된 쿠키로 설정 (확실한 삭제 보장)
|
|
103
|
+
response.cookies.set(cookie.name, '', {
|
|
104
|
+
expires: new Date(0),
|
|
105
|
+
path: '/',
|
|
106
|
+
secure: isProduction,
|
|
107
|
+
httpOnly: true,
|
|
108
|
+
sameSite: isProduction ? 'none' : 'lax',
|
|
109
|
+
...(cookieDomain && { domain: cookieDomain }),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return response;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
75
116
|
// 1. API 요청 처리
|
|
76
117
|
if (pathname.startsWith('/api/')) {
|
|
77
118
|
if (config.authApiPaths.includes(pathname)) {
|