feat: enhance token retrieval for service worker with multi-account support

This commit is contained in:
2026-03-15 17:17:07 +11:00
parent c7bd376292
commit 3cb4237f59
2 changed files with 28 additions and 2 deletions

View File

@@ -32,8 +32,30 @@ if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(swUrl);
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data?.type === 'token' && event.data?.responseKey) {
// Get the token for SW.
const token = localStorage.getItem('cinny_access_token') ?? undefined;
// Get access token from the current session
let token: string | undefined;
// First check the new multi-account session storage
const sessionsJson = localStorage.getItem('matrixSessions');
const currentUserId = localStorage.getItem('currentSessionUserId');
if (sessionsJson && currentUserId) {
try {
const sessions = JSON.parse(sessionsJson);
const currentSession = sessions.find((s: { userId: string }) => s.userId === currentUserId);
if (currentSession?.accessToken) {
token = currentSession.accessToken;
}
} catch {
// JSON parse failed, fall through to fallback
}
}
// Fallback to old single-account storage (for migration)
if (!token) {
token = localStorage.getItem('cinny_access_token') ?? undefined;
}
event.source!.postMessage({
responseKey: event.data.responseKey,
token,