Skip to content

Commit

Permalink
Log out whenever api and keystore are not both logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Dec 3, 2024
1 parent d3b0143 commit 5990646
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/context/SessionContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useContext } from 'react';
import React, { createContext, useCallback, useContext, useEffect } from 'react';

import StatusContext from './StatusContext';
import { BackendApi, useApi } from '../api';
Expand All @@ -25,12 +25,15 @@ export const SessionContextProvider = ({ children }) => {
const api = useApi(isOnline);
const keystore = useLocalStorageKeystore();

const logout = async () => {
// Clear URL parameters
sessionStorage.setItem('freshLogin', 'true');
api.clearSession();
await keystore.close();
};
const logout = useCallback(
async () => {
// Clear URL parameters
sessionStorage.setItem('freshLogin', 'true');
api.clearSession();
await keystore.close();
},
[api, keystore],
);

const value: SessionContextValue = {
api,
Expand All @@ -39,6 +42,16 @@ export const SessionContextProvider = ({ children }) => {
logout,
};

useEffect(
() => {
if (api.isLoggedIn() !== keystore.isOpen()) {
// This might happen if the keystore closes itself due to user inactivity, for example.
logout();
}
},
[api, keystore, logout],
);

return (
<SessionContext.Provider value={value}>
{children}
Expand Down

0 comments on commit 5990646

Please sign in to comment.