Skip to content

Commit

Permalink
feat: use current account for session management
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan authored and ajinkyaraj-23 committed Feb 26, 2025
1 parent 2beab3b commit c84d45b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 22 deletions.
7 changes: 4 additions & 3 deletions apps/web/src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { WalletConnectProvider } from "../WalletConnect/WalletConnectProvider";
export const App = () => {
const { isSessionActive, isOnboarded } = useHandleSession();

if (!isOnboarded()) {
if (!isSessionActive) {
if (isOnboarded()) {
return <SessionLogin />;
}
return <Welcome />;
} else if (!isSessionActive) {
return <SessionLogin />;
} else {
return (
<BeaconProvider>
Expand Down
16 changes: 4 additions & 12 deletions apps/web/src/views/SessionLogin/SessionLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,14 @@ export const useLoginWithMnemonic = (
const mnemonic = (
JSON.parse(accounts.seedPhrases as unknown as string) as Record<string, EncryptedData>
)[defaultAccount.seedFingerPrint];
await decrypt(mnemonic, data.password)
.then(setupPersistence)
.finally(() => {
console.log("Decrypted mnemonic");
window.location.reload();
});
const result = await decrypt(mnemonic, data.password);
setupPersistence(result);
} else if (defaultAccount.type === "secret_key") {
const secretKey = (
JSON.parse(accounts.secretKeys as unknown as string) as Record<RawPkh, EncryptedData>
)[defaultAccount.address.pkh];
await decrypt(secretKey, data.password)
.then(setupPersistence)
.finally(() => {
console.log("Decrypted secret key");
window.location.reload();
});
const result = await decrypt(secretKey, data.password);
setupPersistence(result);
}
},
{ title: "Mnemonic or secret key not found" }
Expand Down
6 changes: 1 addition & 5 deletions packages/state/src/hooks/session.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { useAppDispatch } from "./useAppDispatch";
import { useAppSelector } from "./useAppSelector";
import { setHasSession } from "../slices/session";

const SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes from login

export const useHandleSession = () => {
const isOnboarded = () => !!localStorage.getItem("user_requirements_nonce");
const isSessionActive = useAppSelector(state => state.session.hasSession);
const dispatch = useAppDispatch();
const isSessionActive = useAppSelector(state => !!state.accounts.current);

const setupSessionTimeout = () => {
try {
const timeoutId = setTimeout(() => {
sessionStorage.clear();
dispatch(setHasSession(false));
}, SESSION_TIMEOUT);

// Store timeout ID in case we need to clear it
Expand Down
3 changes: 1 addition & 2 deletions packages/state/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { type Storage, persistReducer, persistStore } from "redux-persist";

import { makePersistConfigs, makeReducer } from "./reducer";
import { accountsSlice } from "./slices/accounts/accounts";
import { setHasSession } from "./slices/session";

// Create initial store without persistence
export const makeStore = () => {
Expand Down Expand Up @@ -56,9 +55,9 @@ export const initializePersistence = (

// Update store's reducer
store.replaceReducer(finalReducer);
store.dispatch(setHasSession(true));

const persistor = persistStore(store);

return { persistor };
};

Expand Down

0 comments on commit c84d45b

Please sign in to comment.