cleanup sessions based on expiration date

This commit is contained in:
peintnermax
2024-04-04 10:23:20 +02:00
parent aa1e20179d
commit 248b543b02
2 changed files with 61 additions and 59 deletions

View File

@@ -130,10 +130,10 @@ export async function createSessionForLoginname(
{
checks: { user: { loginName }, password: { password } },
challenges,
// lifetime: {
// seconds: 300,
// nanos: 0,
// },
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
)
@@ -141,10 +141,10 @@ export async function createSessionForLoginname(
{
checks: { user: { loginName } },
challenges,
// lifetime: {
// seconds: 300,
// nanos: 0,
// },
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
);
@@ -162,10 +162,10 @@ export async function createSessionForUserId(
{
checks: { user: { userId }, password: { password } },
challenges,
// lifetime: {
// seconds: 300,
// nanos: 0,
// },
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
)
@@ -173,10 +173,10 @@ export async function createSessionForUserId(
{
checks: { user: { userId } },
challenges,
// lifetime: {
// seconds: 300,
// nanos: 0,
// },
lifetime: {
seconds: 300,
nanos: 0,
},
},
{}
);

View File

@@ -45,15 +45,15 @@ export async function addSessionToCookie(
currentSessions = [...currentSessions, session];
}
// if (cleanup) {
// const now = new Date();
// const filteredSessions = currentSessions.filter(
// (session) => new Date(session.expirationDate) > now
// );
// return setSessionHttpOnlyCookie(filteredSessions);
// } else {
if (cleanup) {
const now = new Date();
const filteredSessions = currentSessions.filter((session) =>
session.expirationDate ? new Date(session.expirationDate) > now : true
);
return setSessionHttpOnlyCookie(filteredSessions);
} else {
return setSessionHttpOnlyCookie(currentSessions);
// }
}
}
export async function updateSessionCookie(
@@ -72,15 +72,15 @@ export async function updateSessionCookie(
if (foundIndex > -1) {
sessions[foundIndex] = session;
// if (cleanup) {
// const now = new Date();
// const filteredSessions = sessions.filter(
// (session) => new Date(session.expirationDate) > now
// );
// return setSessionHttpOnlyCookie(filteredSessions);
// } else {
if (cleanup) {
const now = new Date();
const filteredSessions = sessions.filter((session) =>
session.expirationDate ? new Date(session.expirationDate) > now : true
);
return setSessionHttpOnlyCookie(filteredSessions);
} else {
return setSessionHttpOnlyCookie(sessions);
// }
}
} else {
throw "updateSessionCookie: session id now found";
}
@@ -98,15 +98,15 @@ export async function removeSessionFromCookie(
: [session];
const reducedSessions = sessions.filter((s) => s.id !== session.id);
// if (cleanup) {
// const now = new Date();
// const filteredSessions = reducedSessions.filter(
// (session) => new Date(session.expirationDate) > now
// );
// return setSessionHttpOnlyCookie(filteredSessions);
// } else {
if (cleanup) {
const now = new Date();
const filteredSessions = reducedSessions.filter((session) =>
session.expirationDate ? new Date(session.expirationDate) > now : true
);
return setSessionHttpOnlyCookie(filteredSessions);
} else {
return setSessionHttpOnlyCookie(reducedSessions);
// }
}
}
export async function getMostRecentSessionCookie(): Promise<any> {
@@ -192,14 +192,16 @@ export async function getAllSessionCookieIds(
if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
// if (cleanup) {
// const now = new Date();
// return sessions
// .filter((session) => new Date(session.expirationDate) > now)
// .map((session) => session.id);
// } else {
if (cleanup) {
const now = new Date();
return sessions
.filter((session) =>
session.expirationDate ? new Date(session.expirationDate) > now : true
)
.map((session) => session.id);
} else {
return sessions.map((session) => session.id);
// }
}
} else {
return [];
}
@@ -219,14 +221,14 @@ export async function getAllSessions(
if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
// if (cleanup) {
// const now = new Date();
// return sessions.filter(
// (session) => new Date(session.expirationDate) > now
// );
// } else {
if (cleanup) {
const now = new Date();
return sessions.filter((session) =>
session.expirationDate ? new Date(session.expirationDate) > now : true
);
} else {
return sessions;
// }
}
} else {
return [];
}