diff --git a/src/app/hooks/useMediaAuthentication.ts b/src/app/hooks/useMediaAuthentication.ts index 6e4d06a..b25f18e 100644 --- a/src/app/hooks/useMediaAuthentication.ts +++ b/src/app/hooks/useMediaAuthentication.ts @@ -1,5 +1,9 @@ import { useSpecVersions } from './useSpecVersions'; +/** + * Determines whether to use authenticated media endpoints. + * @returns true if the server supports authenticated media (Matrix v1.11+) + */ export const useMediaAuthentication = (): boolean => { const { versions, unstable_features: unstableFeatures } = useSpecVersions(); diff --git a/src/index.tsx b/src/index.tsx index 6dc036a..6f21fe4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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,