feat: implement centralized access token management for media downloads and user authentication

This commit is contained in:
2026-03-23 21:53:27 +11:00
parent f76fee23bf
commit 75a9b4ca1e
16 changed files with 183 additions and 54 deletions

View File

@@ -32,33 +32,18 @@ if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(swUrl);
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data?.type === 'token' && event.data?.responseKey) {
// Get access token from the current session
let token: string | undefined;
// Use the centralized token utility to get the current access token
// This ensures we always use the most up-to-date token from the MatrixClient
const getCurrentAccessToken = async () => {
const { getCurrentAccessToken: getToken } = await import('./app/utils/auth');
return getToken();
};
// 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,
getCurrentAccessToken().then(token => {
event.source!.postMessage({
responseKey: event.data.responseKey,
token,
});
});
}
});