longer session duration
This commit is contained in:
parent
eb41dea0b0
commit
f8c8a53ba2
3 changed files with 844 additions and 2 deletions
BIN
logo.png
Normal file
BIN
logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1,005 B |
836
logos.svg
Normal file
836
logos.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 44 KiB |
|
@ -8,10 +8,16 @@ export type Session = {
|
||||||
expires: number; // Millisecond UNIX timestamp
|
expires: number; // Millisecond UNIX timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sessionSeconds = 1800;
|
||||||
|
|
||||||
|
function getExpiryTime() {
|
||||||
|
return Date.now() + sessionSeconds * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
export async function createSession(token: string, userId: number) {
|
export async function createSession(token: string, userId: number) {
|
||||||
const session = {
|
const session = {
|
||||||
userId,
|
userId,
|
||||||
expires: Date.now() + 600 * 1000 // 600 seconds
|
expires: getExpiryTime()
|
||||||
}
|
}
|
||||||
await db.insert(sessions).values({ token, userId, expires: session.expires });
|
await db.insert(sessions).values({ token, userId, expires: session.expires });
|
||||||
return session;
|
return session;
|
||||||
|
@ -21,7 +27,7 @@ export async function createSession(token: string, userId: number) {
|
||||||
export async function validateSession(token: string) {
|
export async function validateSession(token: string) {
|
||||||
const session = await db.select().from(sessions).where(eq(sessions.token, token)).limit(1);
|
const session = await db.select().from(sessions).where(eq(sessions.token, token)).limit(1);
|
||||||
if (session[0] && session[0].expires > Date.now()) {
|
if (session[0] && session[0].expires > Date.now()) {
|
||||||
const newExpires = Date.now() + 600 * 1000;
|
const newExpires = getExpiryTime();
|
||||||
db.update(sessions).set({ expires: newExpires }).where(eq(sessions.token, token)); // refresh the session as long as the user is working in it
|
db.update(sessions).set({ expires: newExpires }).where(eq(sessions.token, token)); // refresh the session as long as the user is working in it
|
||||||
return {
|
return {
|
||||||
...session[0],
|
...session[0],
|
||||||
|
|
Loading…
Add table
Reference in a new issue