@thinkingcat/auth-utils 1.0.28 → 1.0.30
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 +17 -17
- 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) {
|
|
@@ -457,19 +460,12 @@ async function createAuthResponse(accessToken, secret, options) {
|
|
|
457
460
|
jwtId: jwt.id,
|
|
458
461
|
jwtEmail: jwt.email?.substring(0, 20) + '...',
|
|
459
462
|
});
|
|
460
|
-
//
|
|
461
|
-
const displayText = text || serviceId;
|
|
462
|
-
const html = redirectPath
|
|
463
|
-
? createRedirectHTML(redirectPath, displayText)
|
|
464
|
-
: createRedirectHTML('', displayText).replace("window.location.href = ''", "window.location.reload()");
|
|
465
|
-
// 6. Response 생성
|
|
463
|
+
// 4. Response 생성 (HTTP 302 리다이렉트 사용)
|
|
466
464
|
const { NextResponse: NextResponseClass } = await getNextServer();
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
},
|
|
472
|
-
});
|
|
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 });
|
|
473
469
|
// 4. NextAuth session cookie 설정
|
|
474
470
|
const nextAuthCookieName = isProduction
|
|
475
471
|
? '__Secure-next-auth.session-token'
|
|
@@ -656,9 +652,12 @@ async function verifyAndRefreshToken(req, secret, options) {
|
|
|
656
652
|
// 토큰 검증 실패
|
|
657
653
|
}
|
|
658
654
|
debugLog('verifyAndRefreshToken', 'Creating auth response...');
|
|
655
|
+
// 현재 경로로 리다이렉트하여 새로운 쿠키로 다시 요청하도록 함
|
|
656
|
+
const currentPath = req.nextUrl.pathname + req.nextUrl.search;
|
|
659
657
|
const response = await createAuthResponse(refreshResult.accessToken, secret, {
|
|
658
|
+
req,
|
|
660
659
|
refreshToken: newRefreshToken,
|
|
661
|
-
redirectPath:
|
|
660
|
+
redirectPath: currentPath,
|
|
662
661
|
text: text || serviceId,
|
|
663
662
|
cookiePrefix,
|
|
664
663
|
isProduction,
|
|
@@ -1519,6 +1518,7 @@ async function handleMiddleware(req, config, options) {
|
|
|
1519
1518
|
const redirectPath = config.rolePaths[tokenRole] || config.rolePaths[defaultRole] || '/admin';
|
|
1520
1519
|
debugLog('handleMiddleware', `Creating auth response, redirect to: ${redirectPath}`);
|
|
1521
1520
|
const response = await createAuthResponse(tokenParam, secret, {
|
|
1521
|
+
req,
|
|
1522
1522
|
refreshToken: refreshToken || undefined,
|
|
1523
1523
|
redirectPath,
|
|
1524
1524
|
text: serviceId,
|
package/package.json
CHANGED