Skip to content

Commit

Permalink
Send keystore.close event to log out api module too
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Dec 3, 2024
1 parent e122daf commit 9cc6dac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/context/SessionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { createContext, useContext } from 'react';

import StatusContext from './StatusContext';
import { BackendApi, useApi } from '../api';
import { useLocalStorageKeystore } from '../services/LocalStorageKeystore';
import { KeystoreEvent, useLocalStorageKeystore } from '../services/LocalStorageKeystore';
import type { LocalStorageKeystore } from '../services/LocalStorageKeystore';


Expand All @@ -23,7 +23,8 @@ const SessionContext: React.Context<SessionContextValue> = createContext({
export const SessionContextProvider = ({ children }) => {
const { isOnline } = useContext(StatusContext);
const api = useApi(isOnline);
const keystore = useLocalStorageKeystore();
const keystoreEvents = new EventTarget();
const keystore = useLocalStorageKeystore(keystoreEvents);

const logout = async () => {
// Clear URL parameters
Expand All @@ -32,6 +33,8 @@ export const SessionContextProvider = ({ children }) => {
await keystore.close();
};

keystoreEvents.addEventListener(KeystoreEvent.Close, logout, { once: true });

const value: SessionContextValue = {
api,
isLoggedIn: api.isLoggedIn() && keystore.isOpen(),
Expand Down
11 changes: 10 additions & 1 deletion src/services/LocalStorageKeystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export type CachedUser = {
prfKeys: WebauthnPrfSaltInfo[];
}

export enum KeystoreEvent {
/** The keystore has been closed. This event should be propagated to all tabs. */
Close = 'keystore.close',
/** The keystore has been closed. This event should not be propagated to other tabs. */
CloseTabLocal = 'keystore.closeTabLocal',
}

export type CommitCallback = () => Promise<void>;
export interface LocalStorageKeystore {
isOpen(): boolean,
Expand Down Expand Up @@ -75,7 +82,7 @@ export interface LocalStorageKeystore {
}

/** A stateful wrapper around the keystore module, storing state in the browser's localStorage and sessionStorage. */
export function useLocalStorageKeystore(): LocalStorageKeystore {
export function useLocalStorageKeystore(eventTarget: EventTarget): LocalStorageKeystore {
const [cachedUsers, setCachedUsers,] = useLocalStorage<CachedUser[]>("cachedUsers", []);
const [privateData, setPrivateData, clearPrivateData] = useLocalStorage<EncryptedContainer | null>("privateData", null);
const [globalUserHandleB64u, setGlobalUserHandleB64u, clearGlobalUserHandleB64u] = useLocalStorage<string | null>("userHandle", null);
Expand All @@ -97,6 +104,7 @@ export function useLocalStorageKeystore(): LocalStorageKeystore {
const closeTabLocal = useCallback(
() => {
clearSessionStorage();
eventTarget.dispatchEvent(new CustomEvent(KeystoreEvent.CloseTabLocal));
},
[clearSessionStorage],
);
Expand All @@ -107,6 +115,7 @@ export function useLocalStorageKeystore(): LocalStorageKeystore {
clearPrivateData();
clearGlobalUserHandleB64u();
closeTabLocal();
eventTarget.dispatchEvent(new CustomEvent(KeystoreEvent.Close));
},
[closeTabLocal, idb, clearGlobalUserHandleB64u, clearPrivateData],
);
Expand Down

0 comments on commit 9cc6dac

Please sign in to comment.