feat: implement centralized access token management for media downloads and user authentication
This commit is contained in:
@@ -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,
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user