@thinkingcat/auth-utils 1.0.23 → 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
@@ -1256,19 +1256,65 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
1256
1256
  hasValidAccess: hasValidAccessToken,
1257
1257
  hasRefresh: !!refreshToken,
1258
1258
  });
1259
- // NextAuth 토큰 또는 access token 하나라도 유효하면 통과
1260
- if (hasValidNextAuthToken || hasValidAccessToken) {
1261
- debugLog('verifyAndRefreshTokenWithNextAuth', 'At least one token is valid');
1262
- return { isValid: true };
1259
+ // NextAuth cookie와 access token 모두 유효하면 통과
1260
+ if (hasValidNextAuthToken && hasValidAccessToken) {
1261
+ debugLog('verifyAndRefreshTokenWithNextAuth', 'Both NextAuth and access tokens are valid');
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
- // 없으면 refresh token으로 갱신 시도
1265
- if (refreshToken) {
1266
- debugLog('verifyAndRefreshTokenWithNextAuth', 'No valid tokens, attempting refresh');
1280
+ // NextAuth cookie가 없거나 access token 없으면 refresh 시도
1281
+ if (refreshToken && (!hasValidNextAuthToken || !hasValidAccessToken)) {
1282
+ debugLog('verifyAndRefreshTokenWithNextAuth', 'Missing NextAuth or access token, attempting refresh');
1267
1283
  const authCheck = await verifyAndRefreshToken(req, secret, {
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
+ };
1297
+ }
1298
+ // 하나라도 유효하면 일단 통과 (refresh token이 없는 경우)
1299
+ if (hasValidNextAuthToken || hasValidAccessToken) {
1300
+ debugLog('verifyAndRefreshTokenWithNextAuth', 'At least one token is valid (no refresh token)');
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
+ };
1272
1318
  }
1273
1319
  debugLog('verifyAndRefreshTokenWithNextAuth', 'No tokens available');
1274
1320
  return { isValid: false, error: 'NO_TOKEN' };
@@ -1487,28 +1533,21 @@ async function handleMiddleware(req, config, options) {
1487
1533
  const ssoBaseURL = options.ssoBaseURL;
1488
1534
  return await redirectToSSOLogin(req, serviceId, ssoBaseURL);
1489
1535
  }
1490
- // 5. 토큰 확인 변환
1491
- let finalToken = token;
1492
- if (!finalToken && getNextAuthToken) {
1493
- finalToken = await getNextAuthToken(req);
1494
- }
1495
- else if (!finalToken) {
1496
- try {
1497
- const { getToken } = await Promise.resolve().then(() => __importStar(require('next-auth/jwt')));
1498
- finalToken = await getToken({ req, secret });
1499
- }
1500
- catch {
1501
- // NextAuth가 없으면 null 유지
1502
- }
1503
- }
1504
- // verifyAndRefreshToken이 성공했는데 NextAuth 토큰이 없으면, 자체 토큰을 사용
1536
+ // 5. 토큰 확인 - authCheck 결과 재사용 (중복 검증 제거)
1537
+ let finalToken = authCheck.token || token;
1538
+ // authCheck에서 토큰을 반환하지 않았지만 유효한 경우 (드문 케이스)
1505
1539
  if (!finalToken && authCheck.isValid) {
1506
- const accessToken = req.cookies.get(`${cookiePrefix}_access_token`)?.value;
1507
- if (accessToken) {
1508
- const tokenResult = await verifyToken(accessToken, secret);
1509
- if (tokenResult) {
1510
- const { payload } = tokenResult;
1511
- 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 유지
1512
1551
  }
1513
1552
  }
1514
1553
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thinkingcat/auth-utils",
3
- "version": "1.0.23",
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",