Skip to content

Commit

Permalink
Fix cleanSessionStorage on every logout way
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatrakazas committed Dec 5, 2024
1 parent 9cc6dac commit 6fb4f5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/context/SessionContext.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { createContext, useContext } from 'react';
import React, { createContext, useContext, useEffect } from 'react';

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

import keystoreEvents from '../services/keystoreEvents';

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

const logout = async () => {
// Clear URL parameters
const clearSession = async () => {
sessionStorage.setItem('freshLogin', 'true');
api.clearSession();
};

const logout = async () => {
clearSession();
await keystore.close();
};

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

const value: SessionContextValue = {
api,
Expand Down
7 changes: 3 additions & 4 deletions src/services/LocalStorageKeystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export function useLocalStorageKeystore(eventTarget: EventTarget): LocalStorageK
}, []));

const closeTabLocal = useCallback(
() => {
clearSessionStorage();
async (): Promise<void> => {
eventTarget.dispatchEvent(new CustomEvent(KeystoreEvent.CloseTabLocal));
clearSessionStorage();
},
[clearSessionStorage],
[clearSessionStorage, eventTarget],
);

const close = useCallback(
Expand All @@ -115,7 +115,6 @@ export function useLocalStorageKeystore(eventTarget: EventTarget): LocalStorageK
clearPrivateData();
clearGlobalUserHandleB64u();
closeTabLocal();
eventTarget.dispatchEvent(new CustomEvent(KeystoreEvent.Close));
},
[closeTabLocal, idb, clearGlobalUserHandleB64u, clearPrivateData],
);
Expand Down
3 changes: 3 additions & 0 deletions src/services/keystoreEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// keystoreEvents.js
const keystoreEvents = new EventTarget();
export default keystoreEvents;

0 comments on commit 6fb4f5b

Please sign in to comment.