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:
Maurice
2026-10-08 17:32:38 +02:00
parent 1ad6ae0ebc
commit ac89895978
7 changed files with 153 additions and 13 deletions
@@ -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 = ?