Skip to content

Commit 370fb57

Browse files
committed
fix: prevent numeric ID fallback in session creation
- Remove fallback to user.id (numeric) when documentId not found - Add proper error logging when documentId cannot be retrieved - Add debug logging for JWT session validation - Show total session count in blocked request logs for debugging This fixes the "Valid JWT but no active session" error that occurred when sessions were created with numeric user IDs but queried by documentId.
1 parent 3c6c0e8 commit 370fb57

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

server/src/bootstrap.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,25 @@ module.exports = async ({ strapi }) => {
241241
// NOTE: entityService is deprecated, but required here for numeric ID -> documentId conversion
242242
let userDocId = user.documentId;
243243
if (!userDocId && user.id) {
244-
const fullUser = await strapi.entityService.findOne(USER_UID, user.id);
245-
userDocId = fullUser?.documentId || user.id;
244+
const fullUser = await strapi.entityService.findOne(USER_UID, user.id, {
245+
fields: ['documentId'],
246+
});
247+
userDocId = fullUser?.documentId;
248+
249+
if (!userDocId) {
250+
log.error(`[ERROR] Could not get documentId for user ${user.id} - session NOT created!`);
251+
// Continue without creating session - user will need to login again
252+
return;
253+
}
254+
}
255+
256+
if (!userDocId) {
257+
log.error('[ERROR] No user documentId available - cannot create session');
258+
return;
246259
}
247260

261+
log.debug(`[SESSION] Creating session for user documentId: ${userDocId}`);
262+
248263
const newSession = await sessionService.createSession({
249264
userId: userDocId,
250265
ip,
@@ -254,7 +269,11 @@ module.exports = async ({ strapi }) => {
254269
geoData, // Store geolocation data if available
255270
});
256271

257-
log.info(`[SUCCESS] Session created for user ${userDocId} (IP: ${ip})`);
272+
if (newSession?.documentId) {
273+
log.info(`[SUCCESS] Session ${newSession.documentId} created for user ${userDocId} (IP: ${ip})`);
274+
} else {
275+
log.error(`[ERROR] Session creation returned no documentId for user ${userDocId}`);
276+
}
258277

259278
// Advanced: Send notifications
260279
if (geoData && (config.enableEmailAlerts || config.enableWebhooks)) {
@@ -607,18 +626,28 @@ async function registerSessionAwareAuthStrategy(strapi, log) {
607626
}
608627

609628
// Check for active sessions
629+
strapi.log.debug(`[magic-sessionmanager] [JWT] Checking sessions for user: ${userDocId}`);
630+
610631
const activeSessions = await strapi.documents(SESSION_UID).findMany({
611632
filters: {
612633
user: { documentId: userDocId },
613634
isActive: true,
614635
},
615636
limit: 1,
637+
populate: { user: { fields: ['documentId'] } },
616638
});
617639

640+
strapi.log.debug(`[magic-sessionmanager] [JWT] Found ${activeSessions?.length || 0} active sessions`);
641+
618642
// If NO active sessions, return null (invalid token)
619643
if (!activeSessions || activeSessions.length === 0) {
644+
// Debug: Check if ANY sessions exist for this user (including inactive)
645+
const allSessions = await strapi.documents(SESSION_UID).findMany({
646+
filters: { user: { documentId: userDocId } },
647+
limit: 5,
648+
});
620649
strapi.log.info(
621-
`[magic-sessionmanager] [JWT-BLOCKED] Valid JWT but no active session (user: ${userDocId.substring(0, 8)}...)`
650+
`[magic-sessionmanager] [JWT-BLOCKED] Valid JWT but no active session (user: ${userDocId.substring(0, 8)}..., total sessions: ${allSessions?.length || 0})`
622651
);
623652
return null; // This will cause auth to fail
624653
}

0 commit comments

Comments
 (0)