strapi-plugin-magic-sessionmanager 4.2.7 → 4.2.8

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.
@@ -213,16 +213,24 @@ var encryption = {
213
213
  };
214
214
  const SESSION_UID$3 = "plugin::magic-sessionmanager.session";
215
215
  const { decryptToken: decryptToken$3 } = encryption;
216
+ const lastTouchCache = /* @__PURE__ */ new Map();
216
217
  var lastSeen = ({ strapi: strapi2, sessionService }) => {
217
218
  return async (ctx, next) => {
218
- if (ctx.state.user && ctx.state.user.documentId) {
219
- try {
220
- const userId = ctx.state.user.documentId;
221
- const currentToken = ctx.request.headers.authorization?.replace("Bearer ", "");
222
- if (!currentToken) {
223
- await next();
224
- return;
225
- }
219
+ const currentToken = ctx.request.headers.authorization?.replace("Bearer ", "");
220
+ if (!currentToken) {
221
+ await next();
222
+ return;
223
+ }
224
+ const skipPaths = ["/admin", "/_health", "/favicon.ico"];
225
+ if (skipPaths.some((p) => ctx.path.startsWith(p))) {
226
+ await next();
227
+ return;
228
+ }
229
+ let matchingSession = null;
230
+ let userId = null;
231
+ try {
232
+ if (ctx.state.user && ctx.state.user.documentId) {
233
+ userId = ctx.state.user.documentId;
226
234
  const activeSessions = await strapi2.documents(SESSION_UID$3).findMany({
227
235
  filters: {
228
236
  user: { documentId: userId },
@@ -233,7 +241,6 @@ var lastSeen = ({ strapi: strapi2, sessionService }) => {
233
241
  strapi2.log.info(`[magic-sessionmanager] [BLOCKED] User ${userId} has no active sessions`);
234
242
  return ctx.unauthorized("All sessions have been terminated. Please login again.");
235
243
  }
236
- let matchingSession = null;
237
244
  for (const session2 of activeSessions) {
238
245
  if (!session2.token) continue;
239
246
  try {
@@ -249,19 +256,48 @@ var lastSeen = ({ strapi: strapi2, sessionService }) => {
249
256
  strapi2.log.info(`[magic-sessionmanager] [BLOCKED] Session for user ${userId} has been terminated`);
250
257
  return ctx.unauthorized("This session has been terminated. Please login again.");
251
258
  }
259
+ } else {
260
+ const allActiveSessions = await strapi2.documents(SESSION_UID$3).findMany({
261
+ filters: { isActive: true },
262
+ populate: { user: { fields: ["documentId"] } },
263
+ limit: 500
264
+ // Reasonable limit for performance
265
+ });
266
+ for (const session2 of allActiveSessions) {
267
+ if (!session2.token) continue;
268
+ try {
269
+ const decrypted = decryptToken$3(session2.token);
270
+ if (decrypted === currentToken) {
271
+ matchingSession = session2;
272
+ userId = session2.user?.documentId;
273
+ break;
274
+ }
275
+ } catch (err) {
276
+ }
277
+ }
278
+ }
279
+ if (matchingSession) {
252
280
  ctx.state.sessionId = matchingSession.documentId;
253
281
  ctx.state.currentSession = matchingSession;
254
- } catch (err) {
255
- strapi2.log.debug("[magic-sessionmanager] Error checking session:", err.message);
256
282
  }
283
+ } catch (err) {
284
+ strapi2.log.debug("[magic-sessionmanager] Error checking session:", err.message);
257
285
  }
258
286
  await next();
259
- if (ctx.state.user && ctx.state.user.documentId && ctx.state.sessionId) {
287
+ if (matchingSession) {
260
288
  try {
261
- await sessionService.touch({
262
- userId: ctx.state.user.documentId,
263
- sessionId: ctx.state.sessionId
264
- });
289
+ const config2 = strapi2.config.get("plugin::magic-sessionmanager") || {};
290
+ const rateLimit = config2.lastSeenRateLimit || 3e4;
291
+ const now = Date.now();
292
+ const lastTouch = lastTouchCache.get(matchingSession.documentId) || 0;
293
+ if (now - lastTouch > rateLimit) {
294
+ lastTouchCache.set(matchingSession.documentId, now);
295
+ await strapi2.documents(SESSION_UID$3).update({
296
+ documentId: matchingSession.documentId,
297
+ data: { lastActive: /* @__PURE__ */ new Date() }
298
+ });
299
+ strapi2.log.debug(`[magic-sessionmanager] [TOUCH] Session ${matchingSession.documentId} activity updated`);
300
+ }
265
301
  } catch (err) {
266
302
  strapi2.log.debug("[magic-sessionmanager] Error updating lastSeen:", err.message);
267
303
  }
@@ -209,16 +209,24 @@ var encryption = {
209
209
  };
210
210
  const SESSION_UID$3 = "plugin::magic-sessionmanager.session";
211
211
  const { decryptToken: decryptToken$3 } = encryption;
212
+ const lastTouchCache = /* @__PURE__ */ new Map();
212
213
  var lastSeen = ({ strapi: strapi2, sessionService }) => {
213
214
  return async (ctx, next) => {
214
- if (ctx.state.user && ctx.state.user.documentId) {
215
- try {
216
- const userId = ctx.state.user.documentId;
217
- const currentToken = ctx.request.headers.authorization?.replace("Bearer ", "");
218
- if (!currentToken) {
219
- await next();
220
- return;
221
- }
215
+ const currentToken = ctx.request.headers.authorization?.replace("Bearer ", "");
216
+ if (!currentToken) {
217
+ await next();
218
+ return;
219
+ }
220
+ const skipPaths = ["/admin", "/_health", "/favicon.ico"];
221
+ if (skipPaths.some((p) => ctx.path.startsWith(p))) {
222
+ await next();
223
+ return;
224
+ }
225
+ let matchingSession = null;
226
+ let userId = null;
227
+ try {
228
+ if (ctx.state.user && ctx.state.user.documentId) {
229
+ userId = ctx.state.user.documentId;
222
230
  const activeSessions = await strapi2.documents(SESSION_UID$3).findMany({
223
231
  filters: {
224
232
  user: { documentId: userId },
@@ -229,7 +237,6 @@ var lastSeen = ({ strapi: strapi2, sessionService }) => {
229
237
  strapi2.log.info(`[magic-sessionmanager] [BLOCKED] User ${userId} has no active sessions`);
230
238
  return ctx.unauthorized("All sessions have been terminated. Please login again.");
231
239
  }
232
- let matchingSession = null;
233
240
  for (const session2 of activeSessions) {
234
241
  if (!session2.token) continue;
235
242
  try {
@@ -245,19 +252,48 @@ var lastSeen = ({ strapi: strapi2, sessionService }) => {
245
252
  strapi2.log.info(`[magic-sessionmanager] [BLOCKED] Session for user ${userId} has been terminated`);
246
253
  return ctx.unauthorized("This session has been terminated. Please login again.");
247
254
  }
255
+ } else {
256
+ const allActiveSessions = await strapi2.documents(SESSION_UID$3).findMany({
257
+ filters: { isActive: true },
258
+ populate: { user: { fields: ["documentId"] } },
259
+ limit: 500
260
+ // Reasonable limit for performance
261
+ });
262
+ for (const session2 of allActiveSessions) {
263
+ if (!session2.token) continue;
264
+ try {
265
+ const decrypted = decryptToken$3(session2.token);
266
+ if (decrypted === currentToken) {
267
+ matchingSession = session2;
268
+ userId = session2.user?.documentId;
269
+ break;
270
+ }
271
+ } catch (err) {
272
+ }
273
+ }
274
+ }
275
+ if (matchingSession) {
248
276
  ctx.state.sessionId = matchingSession.documentId;
249
277
  ctx.state.currentSession = matchingSession;
250
- } catch (err) {
251
- strapi2.log.debug("[magic-sessionmanager] Error checking session:", err.message);
252
278
  }
279
+ } catch (err) {
280
+ strapi2.log.debug("[magic-sessionmanager] Error checking session:", err.message);
253
281
  }
254
282
  await next();
255
- if (ctx.state.user && ctx.state.user.documentId && ctx.state.sessionId) {
283
+ if (matchingSession) {
256
284
  try {
257
- await sessionService.touch({
258
- userId: ctx.state.user.documentId,
259
- sessionId: ctx.state.sessionId
260
- });
285
+ const config2 = strapi2.config.get("plugin::magic-sessionmanager") || {};
286
+ const rateLimit = config2.lastSeenRateLimit || 3e4;
287
+ const now = Date.now();
288
+ const lastTouch = lastTouchCache.get(matchingSession.documentId) || 0;
289
+ if (now - lastTouch > rateLimit) {
290
+ lastTouchCache.set(matchingSession.documentId, now);
291
+ await strapi2.documents(SESSION_UID$3).update({
292
+ documentId: matchingSession.documentId,
293
+ data: { lastActive: /* @__PURE__ */ new Date() }
294
+ });
295
+ strapi2.log.debug(`[magic-sessionmanager] [TOUCH] Session ${matchingSession.documentId} activity updated`);
296
+ }
261
297
  } catch (err) {
262
298
  strapi2.log.debug("[magic-sessionmanager] Error updating lastSeen:", err.message);
263
299
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "4.2.7",
2
+ "version": "4.2.8",
3
3
  "keywords": [
4
4
  "strapi",
5
5
  "strapi-plugin",