@thinkingcat/auth-utils 1.0.24 → 1.0.25

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 CHANGED
@@ -598,6 +598,7 @@ export declare function verifyAndRefreshTokenWithNextAuth(req: NextRequest, next
598
598
  response?: NextResponse;
599
599
  error?: string;
600
600
  payload?: JWTPayload;
601
+ token?: JWT;
601
602
  }>;
602
603
  /**
603
604
  * 기본 미들웨어 설정을 생성하는 함수
package/dist/index.js CHANGED
@@ -1259,7 +1259,23 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
1259
1259
  // NextAuth cookie와 access token이 모두 유효하면 통과
1260
1260
  if (hasValidNextAuthToken && hasValidAccessToken) {
1261
1261
  debugLog('verifyAndRefreshTokenWithNextAuth', 'Both NextAuth and access tokens are valid');
1262
- return { isValid: true };
1262
+ // payload 추출
1263
+ let payload;
1264
+ if (accessToken) {
1265
+ try {
1266
+ const secretBytes = new TextEncoder().encode(secret);
1267
+ const result = await (0, jose_1.jwtVerify)(accessToken, secretBytes);
1268
+ payload = result.payload;
1269
+ }
1270
+ catch {
1271
+ // 이미 검증됐으므로 실패하지 않을 것
1272
+ }
1273
+ }
1274
+ return {
1275
+ isValid: true,
1276
+ token: nextAuthToken,
1277
+ payload
1278
+ };
1263
1279
  }
1264
1280
  // NextAuth cookie가 없거나 access token이 없으면 refresh 시도
1265
1281
  if (refreshToken && (!hasValidNextAuthToken || !hasValidAccessToken)) {
@@ -1268,12 +1284,37 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
1268
1284
  ...options,
1269
1285
  forceRefresh: true,
1270
1286
  });
1271
- return authCheck;
1287
+ // refresh 후 NextAuth token 재조회 (새로 생성된 cookie에서)
1288
+ let refreshedToken = null;
1289
+ if (authCheck.isValid && authCheck.payload) {
1290
+ // payload에서 JWT 생성
1291
+ refreshedToken = createNextAuthJWT(authCheck.payload, options.serviceId);
1292
+ }
1293
+ return {
1294
+ ...authCheck,
1295
+ token: refreshedToken || undefined
1296
+ };
1272
1297
  }
1273
1298
  // 하나라도 유효하면 일단 통과 (refresh token이 없는 경우)
1274
1299
  if (hasValidNextAuthToken || hasValidAccessToken) {
1275
1300
  debugLog('verifyAndRefreshTokenWithNextAuth', 'At least one token is valid (no refresh token)');
1276
- return { isValid: true };
1301
+ // payload 추출
1302
+ let payload;
1303
+ if (accessToken && hasValidAccessToken) {
1304
+ try {
1305
+ const secretBytes = new TextEncoder().encode(secret);
1306
+ const result = await (0, jose_1.jwtVerify)(accessToken, secretBytes);
1307
+ payload = result.payload;
1308
+ }
1309
+ catch {
1310
+ // 무시
1311
+ }
1312
+ }
1313
+ return {
1314
+ isValid: true,
1315
+ token: nextAuthToken || (payload ? createNextAuthJWT(payload, options.serviceId) : undefined),
1316
+ payload
1317
+ };
1277
1318
  }
1278
1319
  debugLog('verifyAndRefreshTokenWithNextAuth', 'No tokens available');
1279
1320
  return { isValid: false, error: 'NO_TOKEN' };
@@ -1492,28 +1533,21 @@ async function handleMiddleware(req, config, options) {
1492
1533
  const ssoBaseURL = options.ssoBaseURL;
1493
1534
  return await redirectToSSOLogin(req, serviceId, ssoBaseURL);
1494
1535
  }
1495
- // 5. 토큰 확인 변환
1496
- let finalToken = token;
1497
- if (!finalToken && getNextAuthToken) {
1498
- finalToken = await getNextAuthToken(req);
1499
- }
1500
- else if (!finalToken) {
1501
- try {
1502
- const { getToken } = await Promise.resolve().then(() => __importStar(require('next-auth/jwt')));
1503
- finalToken = await getToken({ req, secret });
1504
- }
1505
- catch {
1506
- // NextAuth가 없으면 null 유지
1507
- }
1508
- }
1509
- // verifyAndRefreshToken이 성공했는데 NextAuth 토큰이 없으면, 자체 토큰을 사용
1536
+ // 5. 토큰 확인 - authCheck 결과 재사용 (중복 검증 제거)
1537
+ let finalToken = authCheck.token || token;
1538
+ // authCheck에서 토큰을 반환하지 않았지만 유효한 경우 (드문 케이스)
1510
1539
  if (!finalToken && authCheck.isValid) {
1511
- const accessToken = req.cookies.get(`${cookiePrefix}_access_token`)?.value;
1512
- if (accessToken) {
1513
- const tokenResult = await verifyToken(accessToken, secret);
1514
- if (tokenResult) {
1515
- const { payload } = tokenResult;
1516
- finalToken = createNextAuthJWT(payload, serviceId);
1540
+ debugLog('handleMiddleware', 'authCheck valid but no token, trying to get NextAuth token');
1541
+ if (getNextAuthToken) {
1542
+ finalToken = await getNextAuthToken(req);
1543
+ }
1544
+ else {
1545
+ try {
1546
+ const { getToken } = await Promise.resolve().then(() => __importStar(require('next-auth/jwt')));
1547
+ finalToken = await getToken({ req, secret });
1548
+ }
1549
+ catch {
1550
+ // NextAuth가 없으면 null 유지
1517
1551
  }
1518
1552
  }
1519
1553
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thinkingcat/auth-utils",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "Authentication utilities for ThinkingCat SSO services with conditional logging",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",