@thinkingcat/auth-utils 1.0.27 → 1.0.29
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/index.d.ts +7 -3
- package/dist/index.js +37 -20
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -162,15 +162,19 @@ export declare function createRedirectHTML(redirectPath: string, text: string):
|
|
|
162
162
|
* @param accessToken access token
|
|
163
163
|
* @param secret JWT 서명에 사용할 secret key
|
|
164
164
|
* @param options 추가 옵션
|
|
165
|
+
* @param options.req NextRequest 객체 (필수 - URL origin을 위해 필요)
|
|
165
166
|
* @param options.refreshToken refresh token (선택)
|
|
166
|
-
* @param options.redirectPath 리다이렉트할 경로 (
|
|
167
|
-
* @param options.text
|
|
167
|
+
* @param options.redirectPath 리다이렉트할 경로 (HTTP 302 리다이렉트 사용)
|
|
168
|
+
* @param options.text 응답 메시지 텍스트 (선택사항)
|
|
168
169
|
* @param options.cookiePrefix 쿠키 이름 접두사 (필수)
|
|
169
170
|
* @param options.isProduction 프로덕션 환경 여부 (기본값: false)
|
|
170
171
|
* @param options.cookieDomain 쿠키 도메인 (선택)
|
|
171
|
-
* @
|
|
172
|
+
* @param options.serviceId 서비스 ID (필수)
|
|
173
|
+
* @param options.licenseKey 라이센스 키 (필수)
|
|
174
|
+
* @returns NextResponse 객체 (리다이렉트 또는 JSON 응답)
|
|
172
175
|
*/
|
|
173
176
|
export declare function createAuthResponse(accessToken: string, secret: string, options: {
|
|
177
|
+
req: NextRequest;
|
|
174
178
|
refreshToken?: string;
|
|
175
179
|
redirectPath?: string;
|
|
176
180
|
text?: string;
|
package/dist/index.js
CHANGED
|
@@ -418,17 +418,20 @@ function createRedirectHTML(redirectPath, text) {
|
|
|
418
418
|
* @param accessToken access token
|
|
419
419
|
* @param secret JWT 서명에 사용할 secret key
|
|
420
420
|
* @param options 추가 옵션
|
|
421
|
+
* @param options.req NextRequest 객체 (필수 - URL origin을 위해 필요)
|
|
421
422
|
* @param options.refreshToken refresh token (선택)
|
|
422
|
-
* @param options.redirectPath 리다이렉트할 경로 (
|
|
423
|
-
* @param options.text
|
|
423
|
+
* @param options.redirectPath 리다이렉트할 경로 (HTTP 302 리다이렉트 사용)
|
|
424
|
+
* @param options.text 응답 메시지 텍스트 (선택사항)
|
|
424
425
|
* @param options.cookiePrefix 쿠키 이름 접두사 (필수)
|
|
425
426
|
* @param options.isProduction 프로덕션 환경 여부 (기본값: false)
|
|
426
427
|
* @param options.cookieDomain 쿠키 도메인 (선택)
|
|
427
|
-
* @
|
|
428
|
+
* @param options.serviceId 서비스 ID (필수)
|
|
429
|
+
* @param options.licenseKey 라이센스 키 (필수)
|
|
430
|
+
* @returns NextResponse 객체 (리다이렉트 또는 JSON 응답)
|
|
428
431
|
*/
|
|
429
432
|
async function createAuthResponse(accessToken, secret, options) {
|
|
430
433
|
await checkLicenseKey(options.licenseKey);
|
|
431
|
-
const { refreshToken, redirectPath, text, cookiePrefix, isProduction = false, cookieDomain, serviceId, } = options;
|
|
434
|
+
const { req, refreshToken, redirectPath, text, cookiePrefix, isProduction = false, cookieDomain, serviceId, } = options;
|
|
432
435
|
// 1. 토큰 검증
|
|
433
436
|
const tokenResult = await verifyToken(accessToken, secret);
|
|
434
437
|
if (!tokenResult) {
|
|
@@ -451,20 +454,18 @@ async function createAuthResponse(accessToken, secret, options) {
|
|
|
451
454
|
});
|
|
452
455
|
// 3. NextAuth session cookie 생성
|
|
453
456
|
const nextAuthToken = await encodeNextAuthToken(jwt, secret);
|
|
454
|
-
debugLog('createAuthResponse', 'NextAuth session token encoded'
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
: createRedirectHTML('', displayText).replace("window.location.href = ''", "window.location.reload()");
|
|
460
|
-
// 6. Response 생성
|
|
461
|
-
const { NextResponse: NextResponseClass } = await getNextServer();
|
|
462
|
-
const response = new NextResponseClass(html, {
|
|
463
|
-
status: 200,
|
|
464
|
-
headers: {
|
|
465
|
-
'Content-Type': 'text/html',
|
|
466
|
-
},
|
|
457
|
+
debugLog('createAuthResponse', 'NextAuth session token encoded:', {
|
|
458
|
+
tokenLength: nextAuthToken.length,
|
|
459
|
+
tokenPrefix: nextAuthToken.substring(0, 30) + '...',
|
|
460
|
+
jwtId: jwt.id,
|
|
461
|
+
jwtEmail: jwt.email?.substring(0, 20) + '...',
|
|
467
462
|
});
|
|
463
|
+
// 4. Response 생성 (HTTP 302 리다이렉트 사용)
|
|
464
|
+
const { NextResponse: NextResponseClass } = await getNextServer();
|
|
465
|
+
// redirectPath가 있으면 302 리다이렉트, 없으면 200 OK
|
|
466
|
+
const response = redirectPath
|
|
467
|
+
? NextResponseClass.redirect(new URL(redirectPath, req.url), { status: 302 })
|
|
468
|
+
: NextResponseClass.json({ success: true, message: text || 'Authentication successful' }, { status: 200 });
|
|
468
469
|
// 4. NextAuth session cookie 설정
|
|
469
470
|
const nextAuthCookieName = isProduction
|
|
470
471
|
? '__Secure-next-auth.session-token'
|
|
@@ -480,7 +481,11 @@ async function createAuthResponse(accessToken, secret, options) {
|
|
|
480
481
|
cookieOptions.domain = cookieDomain;
|
|
481
482
|
}
|
|
482
483
|
response.cookies.set(nextAuthCookieName, nextAuthToken, cookieOptions);
|
|
483
|
-
debugLog('createAuthResponse', 'NextAuth session cookie set:',
|
|
484
|
+
debugLog('createAuthResponse', 'NextAuth session cookie set:', {
|
|
485
|
+
name: nextAuthCookieName,
|
|
486
|
+
valueLength: nextAuthToken.length,
|
|
487
|
+
...cookieOptions,
|
|
488
|
+
});
|
|
484
489
|
// 5. 커스텀 토큰 쿠키 설정
|
|
485
490
|
if (refreshToken) {
|
|
486
491
|
setCustomTokens(response, accessToken, refreshToken, {
|
|
@@ -648,6 +653,7 @@ async function verifyAndRefreshToken(req, secret, options) {
|
|
|
648
653
|
}
|
|
649
654
|
debugLog('verifyAndRefreshToken', 'Creating auth response...');
|
|
650
655
|
const response = await createAuthResponse(refreshResult.accessToken, secret, {
|
|
656
|
+
req,
|
|
651
657
|
refreshToken: newRefreshToken,
|
|
652
658
|
redirectPath: '',
|
|
653
659
|
text: text || serviceId,
|
|
@@ -1248,7 +1254,14 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
|
|
|
1248
1254
|
const nextAuthSessionTokenCookieName = isProduction
|
|
1249
1255
|
? '__Secure-next-auth.session-token'
|
|
1250
1256
|
: 'next-auth.session-token';
|
|
1251
|
-
const
|
|
1257
|
+
const nextAuthCookieValue = req.cookies.get(nextAuthSessionTokenCookieName)?.value;
|
|
1258
|
+
const hasNextAuthSessionTokenCookie = !!nextAuthCookieValue;
|
|
1259
|
+
debugLog('verifyAndRefreshTokenWithNextAuth', 'NextAuth cookie check:', {
|
|
1260
|
+
cookieName: nextAuthSessionTokenCookieName,
|
|
1261
|
+
hasCookie: hasNextAuthSessionTokenCookie,
|
|
1262
|
+
cookieLength: nextAuthCookieValue?.length || 0,
|
|
1263
|
+
cookiePrefix: nextAuthCookieValue?.substring(0, 30) + '...' || 'none',
|
|
1264
|
+
});
|
|
1252
1265
|
// NextAuth 토큰 확인
|
|
1253
1266
|
const hasValidNextAuthToken = nextAuthToken && isValidToken(nextAuthToken);
|
|
1254
1267
|
// Access token 확인
|
|
@@ -1430,13 +1443,16 @@ async function handleMiddleware(req, config, options) {
|
|
|
1430
1443
|
let token = null;
|
|
1431
1444
|
if (getNextAuthToken) {
|
|
1432
1445
|
token = await getNextAuthToken(req);
|
|
1446
|
+
debugLog('handleMiddleware', 'Custom getNextAuthToken result:', { hasToken: !!token });
|
|
1433
1447
|
}
|
|
1434
1448
|
else {
|
|
1435
1449
|
try {
|
|
1436
1450
|
const { getToken } = await Promise.resolve().then(() => __importStar(require('next-auth/jwt')));
|
|
1437
1451
|
token = await getToken({ req, secret });
|
|
1452
|
+
debugLog('handleMiddleware', 'getToken result:', { hasToken: !!token });
|
|
1438
1453
|
}
|
|
1439
|
-
catch {
|
|
1454
|
+
catch (error) {
|
|
1455
|
+
debugLog('handleMiddleware', 'getToken failed:', error);
|
|
1440
1456
|
// NextAuth가 없으면 null 유지
|
|
1441
1457
|
}
|
|
1442
1458
|
}
|
|
@@ -1500,6 +1516,7 @@ async function handleMiddleware(req, config, options) {
|
|
|
1500
1516
|
const redirectPath = config.rolePaths[tokenRole] || config.rolePaths[defaultRole] || '/admin';
|
|
1501
1517
|
debugLog('handleMiddleware', `Creating auth response, redirect to: ${redirectPath}`);
|
|
1502
1518
|
const response = await createAuthResponse(tokenParam, secret, {
|
|
1519
|
+
req,
|
|
1503
1520
|
refreshToken: refreshToken || undefined,
|
|
1504
1521
|
redirectPath,
|
|
1505
1522
|
text: serviceId,
|
package/package.json
CHANGED