All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m53s
Every preview shares the origin minds.gamecult.org and so one localStorage. A login another preview left (cinny_hs_base_url of a real homeserver) made the demo connect to that server, which the guard refuses, so SpecVersions showed 'Unable to connect to the homeserver'. installDemo now drops any stored session whose base is not the demo origin, and the service worker is registered after that so it is never handed the dropped session. Replaces the foreign-session banner text. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
140 lines
3.7 KiB
TypeScript
140 lines
3.7 KiB
TypeScript
// import { atom } from 'jotai';
|
|
// import {
|
|
// atomWithLocalStorage,
|
|
// getLocalStorageItem,
|
|
// setLocalStorageItem,
|
|
// } from './utils/atomWithLocalStorage';
|
|
|
|
export type Session = {
|
|
baseUrl: string;
|
|
userId: string;
|
|
deviceId: string;
|
|
accessToken: string;
|
|
expiresInMs?: number;
|
|
refreshToken?: string;
|
|
fallbackSdkStores?: boolean;
|
|
};
|
|
|
|
export type Sessions = Session[];
|
|
export type SessionStoreName = {
|
|
sync: string;
|
|
crypto: string;
|
|
};
|
|
|
|
/**
|
|
* Migration code for old session
|
|
*/
|
|
// const FALLBACK_STORE_NAME: SessionStoreName = {
|
|
// sync: 'web-sync-store',
|
|
// crypto: 'crypto-store',
|
|
// } as const;
|
|
|
|
export function setFallbackSession(
|
|
accessToken: string,
|
|
deviceId: string,
|
|
userId: string,
|
|
baseUrl: string
|
|
) {
|
|
localStorage.setItem('cinny_access_token', accessToken);
|
|
localStorage.setItem('cinny_device_id', deviceId);
|
|
localStorage.setItem('cinny_user_id', userId);
|
|
localStorage.setItem('cinny_hs_base_url', baseUrl);
|
|
}
|
|
export const removeFallbackSession = (storage: Storage = localStorage) => {
|
|
storage.removeItem('cinny_hs_base_url');
|
|
storage.removeItem('cinny_user_id');
|
|
storage.removeItem('cinny_device_id');
|
|
storage.removeItem('cinny_access_token');
|
|
};
|
|
export const getFallbackSession = (storage: Storage = localStorage): Session | undefined => {
|
|
const baseUrl = storage.getItem('cinny_hs_base_url');
|
|
const userId = storage.getItem('cinny_user_id');
|
|
const deviceId = storage.getItem('cinny_device_id');
|
|
const accessToken = storage.getItem('cinny_access_token');
|
|
|
|
if (baseUrl && userId && deviceId && accessToken) {
|
|
const session: Session = {
|
|
baseUrl,
|
|
userId,
|
|
deviceId,
|
|
accessToken,
|
|
fallbackSdkStores: true,
|
|
};
|
|
|
|
return session;
|
|
}
|
|
|
|
return undefined;
|
|
};
|
|
/**
|
|
* End of migration code for old session
|
|
*/
|
|
|
|
// export const getSessionStoreName = (session: Session): SessionStoreName => {
|
|
// if (session.fallbackSdkStores) {
|
|
// return FALLBACK_STORE_NAME;
|
|
// }
|
|
|
|
// return {
|
|
// sync: `sync${session.userId}`,
|
|
// crypto: `crypto${session.userId}`,
|
|
// };
|
|
// };
|
|
|
|
// export const MATRIX_SESSIONS_KEY = 'matrixSessions';
|
|
// const baseSessionsAtom = atomWithLocalStorage<Sessions>(
|
|
// MATRIX_SESSIONS_KEY,
|
|
// (key) => {
|
|
// const defaultSessions: Sessions = [];
|
|
// const sessions = getLocalStorageItem(key, defaultSessions);
|
|
|
|
// // Before multi account support session was stored
|
|
// // as multiple item in local storage.
|
|
// // So we need these migration code.
|
|
// const fallbackSession = getFallbackSession();
|
|
// if (fallbackSession) {
|
|
// removeFallbackSession();
|
|
// sessions.push(fallbackSession);
|
|
// setLocalStorageItem(key, sessions);
|
|
// }
|
|
// return sessions;
|
|
// },
|
|
// (key, value) => {
|
|
// setLocalStorageItem(key, value);
|
|
// }
|
|
// );
|
|
|
|
// export type SessionsAction =
|
|
// | {
|
|
// type: 'PUT';
|
|
// session: Session;
|
|
// }
|
|
// | {
|
|
// type: 'DELETE';
|
|
// session: Session;
|
|
// };
|
|
|
|
// export const sessionsAtom = atom<Sessions, [SessionsAction], undefined>(
|
|
// (get) => get(baseSessionsAtom),
|
|
// (get, set, action) => {
|
|
// if (action.type === 'PUT') {
|
|
// const sessions = [...get(baseSessionsAtom)];
|
|
// const sessionIndex = sessions.findIndex(
|
|
// (session) => session.userId === action.session.userId
|
|
// );
|
|
// if (sessionIndex === -1) {
|
|
// sessions.push(action.session);
|
|
// } else {
|
|
// sessions.splice(sessionIndex, 1, action.session);
|
|
// }
|
|
// set(baseSessionsAtom, sessions);
|
|
// return;
|
|
// }
|
|
// if (action.type === 'DELETE') {
|
|
// const sessions = get(baseSessionsAtom).filter(
|
|
// (session) => session.userId !== action.session.userId
|
|
// );
|
|
// set(baseSessionsAtom, sessions);
|
|
// }
|
|
// }
|
|
// );
|