fix: renew session if there are less than 3 days left
Some checks failed
deploy to cloudflare pages / deploy (push) Failing after 13s
Some checks failed
deploy to cloudflare pages / deploy (push) Failing after 13s
This commit is contained in:
parent
8d7b3d6dc2
commit
b4ad43147b
1 changed files with 12 additions and 2 deletions
|
@ -66,9 +66,19 @@ export async function validateSessionToken(
|
|||
if (Date.now() >= session.expiresAt.getTime()) {
|
||||
await invalidateSession(sessionId, db);
|
||||
return { session: null, user: null };
|
||||
} else {
|
||||
return { session, user };
|
||||
}
|
||||
|
||||
// if less than 3 days left
|
||||
if (Date.now() >= session.expiresAt.getTime() - 1000 * 60 * 60 * 24 * 3) {
|
||||
session.expiresAt = new Date(Date.now() + 1000 * 60 * 60 * 24 * 7); // 7 days
|
||||
|
||||
await db
|
||||
.prepare("UPDATE session SET expires_at = ? WHERE id = ?")
|
||||
.bind(Math.floor(session.expiresAt.getTime() / 1000), session.id)
|
||||
.run();
|
||||
}
|
||||
|
||||
return { session, user };
|
||||
}
|
||||
|
||||
export async function invalidateSession(sessionId: string, db: D1Database): Promise<void> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue