Improve remote Matrix token refresh

This commit is contained in:
jjbrunton committed 2026-08-08 14:30:40 +01:00
1 parent b9df8459e3
commit bffda95251
6 files changed
+226 -14

No files matched your search

+9 -10
View File
@@ -22,9 +22,9 @@ import {
ChatService,
MatrixClient,
MatrixMediaDownloader,
MatrixTokenRefresher,
extractMatrixCredentials,
loadCachedCredentials,
type TokenRefresher,
type UsernameResolver,
type MatrixCredentials,
} from './services/chat/index.js';
@@ -187,16 +187,15 @@ export async function bootstrap(config: Config, logger: Logger): Promise<AppCont
const mediaDownloader = new MatrixMediaDownloader(matrixClient, logger);
const mediaService = new MediaService(mediaDownloader, logger);
// 3. Token refresher — on 401, re-sniff via the running browser session
// and swap the new credentials into the chat service.
// 3. Token refresher — on 401, restore the persistent Reddit session when
// possible, re-sniff once, and swap credentials into the chat service.
// Concurrent callers share that refresh rather than opening extra pages.
let chatService: ChatService;
const tokenRefresher: TokenRefresher = {
async refresh() {
const fresh = await extractMatrixCredentials(browserClient.getContext()!, logger);
const next = new MatrixClient(fresh, logger);
chatService.setMatrixClient(next);
},
};
const tokenRefresher = new MatrixTokenRefresher(
browserClient,
logger,
(fresh) => chatService.setMatrixClient(new MatrixClient(fresh, logger)),
);
// 4. Username resolver — turns t2 IDs into real Reddit usernames so the
// dashboard isn't full of `t2_abc123`. Uses Reddit's bulk endpoint.