mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
fix(sessions): renew a token from before tracking into one session
A token without a jti past half its life is renewed on every request its cookie authenticates, and each renewal minted a session with a random id. A first page load sends many requests at once, so one browser ended up with several active sessions; only the last cookie survived, and the rest showed up in GET /api/auth/sessions as separate devices until they expired, up to 30 days for remember me. The interceptor now hands the token to SessionsService.renew, which derives the session id from it (a SHA-256 of the token, laid out as a version 8 UUID so it passes the same id check) and inserts the row only if it is not there yet. Every renewal of the same token lands on the same session; if that session was ended in the meantime nothing is renewed, just as for a tracked token. A caller that passes no token gets the old behaviour.
This commit is contained in:
@@ -56,6 +56,25 @@ export class UserSessionsRepository extends TrekRepository<UserSessions> {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* `INSERT INTO user_sessions (id, user_id, created_at, last_seen_at, expires_at, user_agent)
|
||||
* VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO NOTHING`: a row that is
|
||||
* already there, ended or not, is left exactly as it is.
|
||||
*/
|
||||
async insertSessionIfAbsent(row: NewUserSessionRow): Promise<void> {
|
||||
await this.upsertMany(
|
||||
[{
|
||||
id: row.id,
|
||||
user: row.user_id,
|
||||
created_at: row.created_at,
|
||||
last_seen_at: row.created_at,
|
||||
expires_at: row.expires_at,
|
||||
user_agent: row.user_agent,
|
||||
}],
|
||||
{ onConflictFields: ['id'], onConflictAction: 'ignore' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The check behind every session token that carries an id:
|
||||
* `SELECT id, last_seen_at FROM user_sessions WHERE id = ? AND user_id = ?
|
||||
|
||||
Reference in New Issue
Block a user