Files
klz-cables.com/.pnpm-store/v10/files/19/e8469bfea0b1bf686ddc7ddd0152b77862245036945b8066faec3883d8809e952851bd897c586c043bae4eb92eaa237748b2cf7b4ead9e5d44266386982f04
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

64 lines
2.0 KiB
Plaintext

import { v4 as uuid } from 'uuid';
/**
* Removes expired sessions from an array of sessions
*/ export const removeExpiredSessions = (sessions)=>{
const now = new Date();
return sessions.filter(({ expiresAt })=>{
const expiry = expiresAt instanceof Date ? expiresAt : new Date(expiresAt);
return expiry > now;
});
};
/**
* Adds a session to the user and removes expired sessions
* @returns The session ID (sid) if sessions are used
*/ export const addSessionToUser = async ({ collectionConfig, payload, req, user })=>{
let sid;
if (collectionConfig.auth.useSessions) {
// Add session to user
sid = uuid();
const now = new Date();
const tokenExpInMs = collectionConfig.auth.tokenExpiration * 1000;
const expiresAt = new Date(now.getTime() + tokenExpInMs);
const session = {
id: sid,
createdAt: now,
expiresAt
};
if (!user.sessions?.length) {
user.sessions = [
session
];
} else {
user.sessions = removeExpiredSessions(user.sessions);
user.sessions.push(session);
}
// Prevent updatedAt from being updated when only adding a session
user.updatedAt = null;
await payload.db.updateOne({
id: user.id,
collection: collectionConfig.slug,
data: user,
req,
returning: false
});
user.collection = collectionConfig.slug;
user._strategy = 'local-jwt';
}
return {
sid
};
};
export const revokeSession = async ({ collectionConfig, payload, req, sid, user })=>{
if (collectionConfig.auth.useSessions && user && user.sessions?.length) {
user.sessions = user.sessions.filter((session)=>session.id !== sid);
await payload.db.updateOne({
id: user.id,
collection: collectionConfig.slug,
data: user,
req,
returning: false
});
}
};
//# sourceMappingURL=sessions.js.map