@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.
- package/dist/index.js +38 -10
- 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
|
-
|
|
429
|
-
debugLog('createAuthResponse', '
|
|
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
|
|
1237
|
-
if (hasValidNextAuthToken
|
|
1238
|
-
debugLog('verifyAndRefreshTokenWithNextAuth', '
|
|
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
|
-
//
|
|
1242
|
-
if (refreshToken) {
|
|
1243
|
-
debugLog('verifyAndRefreshTokenWithNextAuth', '
|
|
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