@thinkingcat/auth-utils 1.0.22 → 1.0.24

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.
Files changed (2) hide show
  1. package/dist/index.js +38 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -419,14 +419,21 @@ async function createAuthResponse(accessToken, secret, options) {
419
419
  const { payload } = tokenResult;
420
420
  // 2. NextAuth JWT 생성
421
421
  const jwt = createNextAuthJWT(payload, serviceId);
422
+ // refreshToken 추가
423
+ if (refreshToken) {
424
+ jwt.refreshToken = refreshToken;
425
+ }
426
+ // accessTokenExpires 추가 (15분)
427
+ jwt.accessTokenExpires = Date.now() + (15 * 60 * 1000);
422
428
  debugLog('createAuthResponse', 'JWT created:', {
423
429
  hasId: !!jwt.id,
424
430
  hasEmail: !!jwt.email,
425
431
  hasRole: !!jwt.role,
432
+ hasRefreshToken: !!jwt.refreshToken,
426
433
  });
427
- // 3. NextAuth 세션 토큰 생성 전략
428
- // NextAuth의 JWT 콜백이 custom tokens를 읽어서 자동으로 NextAuth 세션을 생성
429
- debugLog('createAuthResponse', 'Custom tokens will be set, NextAuth JWT callback will handle session creation');
434
+ // 3. NextAuth session cookie 생성
435
+ const nextAuthToken = await encodeNextAuthToken(jwt, secret);
436
+ debugLog('createAuthResponse', 'NextAuth session token encoded');
430
437
  // 5. HTML 생성
431
438
  const displayText = text || serviceId;
432
439
  const html = redirectPath
@@ -440,7 +447,23 @@ async function createAuthResponse(accessToken, secret, options) {
440
447
  'Content-Type': 'text/html',
441
448
  },
442
449
  });
443
- // 4. 쿠키 설정
450
+ // 4. NextAuth session cookie 설정
451
+ const nextAuthCookieName = isProduction
452
+ ? '__Secure-next-auth.session-token'
453
+ : 'next-auth.session-token';
454
+ const cookieOptions = {
455
+ httpOnly: true,
456
+ secure: isProduction,
457
+ sameSite: isProduction ? 'none' : 'lax',
458
+ path: '/',
459
+ maxAge: 30 * 24 * 60 * 60, // 30일
460
+ };
461
+ if (cookieDomain) {
462
+ cookieOptions.domain = cookieDomain;
463
+ }
464
+ response.cookies.set(nextAuthCookieName, nextAuthToken, cookieOptions);
465
+ debugLog('createAuthResponse', 'NextAuth session cookie set:', nextAuthCookieName);
466
+ // 5. 커스텀 토큰 쿠키 설정
444
467
  if (refreshToken) {
445
468
  setCustomTokens(response, accessToken, refreshToken, {
446
469
  cookiePrefix,
@@ -1233,20 +1256,25 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
1233
1256
  hasValidAccess: hasValidAccessToken,
1234
1257
  hasRefresh: !!refreshToken,
1235
1258
  });
1236
- // NextAuth 토큰 또는 access token 하나라도 유효하면 통과
1237
- if (hasValidNextAuthToken || hasValidAccessToken) {
1238
- debugLog('verifyAndRefreshTokenWithNextAuth', 'At least one token is valid');
1259
+ // NextAuth cookie와 access token 모두 유효하면 통과
1260
+ if (hasValidNextAuthToken && hasValidAccessToken) {
1261
+ debugLog('verifyAndRefreshTokenWithNextAuth', 'Both NextAuth and access tokens are valid');
1239
1262
  return { isValid: true };
1240
1263
  }
1241
- // 없으면 refresh token으로 갱신 시도
1242
- if (refreshToken) {
1243
- debugLog('verifyAndRefreshTokenWithNextAuth', 'No valid tokens, attempting refresh');
1264
+ // NextAuth cookie가 없거나 access token 없으면 refresh 시도
1265
+ if (refreshToken && (!hasValidNextAuthToken || !hasValidAccessToken)) {
1266
+ debugLog('verifyAndRefreshTokenWithNextAuth', 'Missing NextAuth or access token, attempting refresh');
1244
1267
  const authCheck = await verifyAndRefreshToken(req, secret, {
1245
1268
  ...options,
1246
1269
  forceRefresh: true,
1247
1270
  });
1248
1271
  return authCheck;
1249
1272
  }
1273
+ // 하나라도 유효하면 일단 통과 (refresh token이 없는 경우)
1274
+ if (hasValidNextAuthToken || hasValidAccessToken) {
1275
+ debugLog('verifyAndRefreshTokenWithNextAuth', 'At least one token is valid (no refresh token)');
1276
+ return { isValid: true };
1277
+ }
1250
1278
  debugLog('verifyAndRefreshTokenWithNextAuth', 'No tokens available');
1251
1279
  return { isValid: false, error: 'NO_TOKEN' };
1252
1280
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thinkingcat/auth-utils",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
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",