fix(sessions): carry the signed-in sessions across a demo reset and a restore

Both swap the whole database file. The demo reset copies a baseline
saved at first seed, before anyone logged in, so it brings back an empty
user_sessions table; a backup restore brings the backup's own rows, or
none for a backup from before sessions were tracked. A session token
whose row is missing is refused, so the hourly reset signed every demo
visitor and the demo admin out, and the admin who ran a restore got a
401 on the next request. Before session tracking only the password
version applied, and those tokens survived.

Both paths now read the active rows with their owner's email before the
swap and put them back after the reopen, in a fresh fork, for every
account the new file still holds under the same id and email. A row the
new file already has is left as it is, so a session that ended there
stays ended, and a session whose id now names somebody else (a backup
from another install) is dropped instead of handed to them. The password
version check still refuses a token the restored account no longer
matches. Reading or writing them is best effort and never fails the
reset or the restore.
This commit is contained in:
Maurice
2026-10-08 17:24:26 +02:00
parent 9aab9e7513
commit 474beb0dfb
9 changed files with 379 additions and 0 deletions
@@ -1,4 +1,5 @@
import type { UserSessions } from '../entities/UserSessions.entity';
import { Users } from '../entities/Users.entity';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
@@ -18,6 +19,14 @@ const _userSessionRowKeys: AssertRowKeys<UserSessionRow, UserSessions> = true;
/** What a session list shows: the row without its owner, who is the caller. */
export type UserSessionListRow = Omit<UserSessionRow, 'user_id' | 'revoked_at'>;
/**
* An active session read before a database file swap, with the email its
* owner had then, so it is put back only for that same account.
*/
export interface CarriedUserSessionRow extends Omit<UserSessionRow, 'revoked_at'> {
email: string;
}
/** The columns a new sign-in writes; `last_seen_at` starts at `created_at`. */
export interface NewUserSessionRow {
id: string;
@@ -124,6 +133,70 @@ export class UserSessionsRepository extends TrekRepository<UserSessions> {
);
}
/**
* Every active session with its owner's email, read before the database
* file is swapped (a demo reset, a backup restore):
* `SELECT s.* FROM user_sessions s WHERE s.revoked_at IS NULL AND s.expires_at > ?`,
* then `SELECT id, email FROM users WHERE id IN (...)`.
*/
async listActiveToCarry(now: string): Promise<CarriedUserSessionRow[]> {
const rows = await this.find({ revoked_at: null, expires_at: { $gt: now } }, { orderBy: { id: 'asc' } });
if (rows.length === 0) return [];
const emails = await this.ownerEmails(rows.map((row) => row.user.id));
const carried: CarriedUserSessionRow[] = [];
for (const row of rows) {
const email = emails.get(row.user.id);
if (email === undefined) continue;
carried.push({
id: row.id,
user_id: row.user.id,
email,
created_at: row.created_at,
last_seen_at: row.last_seen_at,
expires_at: row.expires_at,
user_agent: row.user_agent ?? null,
});
}
return carried;
}
/**
* Put the sessions `listActiveToCarry` read back after the swap:
* `INSERT INTO user_sessions (...) VALUES (...) ON CONFLICT (id) DO NOTHING`
* for each one whose user is still there under the same id and the same
* email. A row the swapped-in file already holds is left as it is, revoked
* or not, and a session whose id now belongs to somebody else (a backup
* from another install) is dropped rather than handed to them. Answers how
* many were offered for insert.
*/
async restoreCarried(rows: readonly CarriedUserSessionRow[]): Promise<number> {
if (rows.length === 0) return 0;
const emails = await this.ownerEmails(rows.map((row) => row.user_id));
const kept = rows.filter((row) => emails.get(row.user_id) === row.email);
if (kept.length === 0) return 0;
await this.upsertMany(
kept.map((row) => ({
id: row.id,
user: row.user_id,
created_at: row.created_at,
last_seen_at: row.last_seen_at,
expires_at: row.expires_at,
revoked_at: null,
user_agent: row.user_agent,
})),
{ onConflictFields: ['id'], onConflictAction: 'ignore' },
);
return kept.length;
}
/** `SELECT id, email FROM users WHERE id IN (...)`, as a map. */
private async ownerEmails(userIds: readonly number[]): Promise<Map<number, string>> {
const owners = await this.getEntityManager()
.getRepository(Users)
.find({ id: { $in: [...new Set(userIds)] } }, { fields: ['id', 'email'] });
return new Map(owners.map((owner) => [owner.id, owner.email]));
}
/**
* The nightly purge: `DELETE FROM user_sessions WHERE expires_at <= ? OR revoked_at IS NOT NULL`.
* A row that is gone refuses its token just as a revoked one does, so