diff options
Diffstat (limited to 'app/src/lib/server/sessions.ts')
-rw-r--r-- | app/src/lib/server/sessions.ts | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/app/src/lib/server/sessions.ts b/app/src/lib/server/sessions.ts index 69a8b46..502ab74 100644 --- a/app/src/lib/server/sessions.ts +++ b/app/src/lib/server/sessions.ts @@ -51,14 +51,14 @@ function encodeBase32LowerCaseNoPadding(input: Uint8Array): string { const VALID_MILLISECONDS = 1000 * 60 * 60 * 24 * 30; /** Creates a new session for the user with the given `userId`. */ -export async function createSession(dbConn: pg.ClientBase, userId: number): Promise<Session> { +export async function createSession(dbClient: pg.ClientBase, userId: number): Promise<Session> { const token = generateSessionToken(); const session: Session = { token: token, userId, expiresAt: new Date(Date.now() + VALID_MILLISECONDS), }; - await dbConn.query("INSERT INTO sessions(token, user_id, expires_at) VALUES ($1, $2, $3);", [ + await dbClient.query("INSERT INTO sessions(token, user_id, expires_at) VALUES ($1, $2, $3);", [ session.token, session.userId, session.expiresAt, @@ -78,11 +78,11 @@ export async function createSession(dbConn: pg.ClientBase, userId: number): Prom * @returns A session + user pair if the session is valid, or `null` for both otherwise. */ export async function validateSessionToken( - dbConn: pg.ClientBase, + dbClient: pg.ClientBase, token: string, ): Promise<SessionValidationResult> { // Step 1 - const result = await dbConn.query( + const result = await dbClient.query( `SELECT * FROM sessions INNER JOIN users ON users.id = sessions.user_id WHERE token = $1;`, @@ -110,7 +110,7 @@ export async function validateSessionToken( // Step 2. const now = Date.now(); if (now >= session.expiresAt.getTime()) { - await invalidateSession(dbConn, session.token); + await invalidateSession(dbClient, session.token); return { session: null, user: null }; } @@ -118,7 +118,7 @@ export async function validateSessionToken( // We only do this a bit into the period to avoid superflous database writes. if (now >= session.expiresAt.getTime() - VALID_MILLISECONDS / 2) { session.expiresAt = new Date(session.expiresAt.getTime() + VALID_MILLISECONDS / 2); - await dbConn.query("UPDATE sessions SET expires_at = ? WHERE id = ?;", [ + await dbClient.query("UPDATE sessions SET expires_at = ? WHERE id = ?;", [ session.expiresAt, session.token, ]); @@ -129,13 +129,16 @@ export async function validateSessionToken( /** Invalidates the session with token `sessionToken`. */ export async function invalidateSession( - dbConn: pg.ClientBase, + dbClient: pg.ClientBase, sessionToken: string, ): Promise<void> { - await dbConn.query("DELETE sessions WHERE token = ?;", [sessionToken]); + await dbClient.query("DELETE sessions WHERE token = ?;", [sessionToken]); } /** Invalidates all sessions for the user with the id `userId`. */ -export async function invalidateAllSessions(dbConn: pg.ClientBase, userId: number): Promise<void> { - await dbConn.query("DELETE sessions WHERE user_id = ?", [userId]); +export async function invalidateAllSessions( + dbClient: pg.ClientBase, + userId: number, +): Promise<void> { + await dbClient.query("DELETE sessions WHERE user_id = ?", [userId]); } |