From cf6729603e6378acbd1fde112c9e83f6972f5985 Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Fri, 9 Aug 2024 10:42:06 +0530 Subject: [PATCH] Persist subscriber ID in local storage --- src/pages/ConnectWallet.tsx | 4 +--- src/pages/OnboardingSuccess.tsx | 2 ++ src/pages/SignWithCosmos.tsx | 4 ++-- src/pages/SignWithNitroKey.tsx | 21 ++++++++++----------- src/pages/Thanks.tsx | 15 +++++++++------ src/pages/UserVerification.tsx | 3 ++- 6 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/pages/ConnectWallet.tsx b/src/pages/ConnectWallet.tsx index a0a83dd..1550f77 100644 --- a/src/pages/ConnectWallet.tsx +++ b/src/pages/ConnectWallet.tsx @@ -15,9 +15,7 @@ const ConnectWallet = () => { useEffect(() => { if (session) { - navigate("/sign-with-nitro-key", { - state: location.state - }); + navigate("/sign-with-nitro-key"); } }, [session, navigate, location]); diff --git a/src/pages/OnboardingSuccess.tsx b/src/pages/OnboardingSuccess.tsx index c06bdb5..129d724 100644 --- a/src/pages/OnboardingSuccess.tsx +++ b/src/pages/OnboardingSuccess.tsx @@ -48,6 +48,8 @@ const OnboardingSuccess = () => { return; } + localStorage.removeItem('subscriberIdHash'); + setParticipant(participant); } catch (error) { console.error("Error fetching participants", error); diff --git a/src/pages/SignWithCosmos.tsx b/src/pages/SignWithCosmos.tsx index 46a1db8..24f279e 100644 --- a/src/pages/SignWithCosmos.tsx +++ b/src/pages/SignWithCosmos.tsx @@ -25,17 +25,17 @@ const SignWithCosmos = () => { const navigate = useNavigate(); - const {message: innerMessage, cosmosAddress, receivedEthSig: ethSignature, subscriberIdHash} = location.state as { + const {message: innerMessage, cosmosAddress, receivedEthSig: ethSignature} = location.state as { message?: { msg: string; address: string; }; cosmosAddress?: string; receivedEthSig?: string; - subscriberIdHash?: string; }; const ethAddress = innerMessage!.address; + const subscriberIdHash = localStorage.getItem('subscriberIdHash'); const createCosmosClient = useCallback(async (endpoint: string) => { return await StargateClient.connect(endpoint); diff --git a/src/pages/SignWithNitroKey.tsx b/src/pages/SignWithNitroKey.tsx index 51b9754..eb92c55 100644 --- a/src/pages/SignWithNitroKey.tsx +++ b/src/pages/SignWithNitroKey.tsx @@ -1,5 +1,5 @@ import React, { useState, useMemo, useEffect } from "react"; -import { useLocation, useNavigate } from "react-router-dom"; +import { useNavigate } from "react-router-dom"; import { enqueueSnackbar } from "notistack"; import canonicalStringify from "canonical-json"; @@ -27,7 +27,6 @@ const SignWithNitroKey = () => { }, [session, signClient, checkPersistedState]); const navigate = useNavigate(); - const location = useLocation(); const [ethAddress, setEthAddress] = useState(""); const [ethSignature, setEthSignature] = useState(""); @@ -35,6 +34,15 @@ const SignWithNitroKey = () => { const [isLoading, setIsLoading] = useState(false); + const subscriberIdHash = localStorage.getItem('subscriberIdHash'); + + useEffect(()=>{ + if(!subscriberIdHash){ + setIsLoading(false) + enqueueSnackbar("Subscriber ID not found. Please verify your email and try again", { variant: "error" }); + } + }, [subscriberIdHash]) + const message = useMemo(() => { return { msg: "Register my account as a participant on the Laconic network", @@ -68,20 +76,11 @@ const SignWithNitroKey = () => { }, }); } else { - const state = location.state as { - subscriberIdHash?: string - } - - if (!state.subscriberIdHash) { - throw new Error("Subscriber ID not found. Please verify your email and try again") - } - navigate("/sign-with-cosmos", { state: { message, cosmosAddress, receivedEthSig, - subscriberIdHash: state.subscriberIdHash, }, }); } diff --git a/src/pages/Thanks.tsx b/src/pages/Thanks.tsx index c8a18f0..983e89f 100644 --- a/src/pages/Thanks.tsx +++ b/src/pages/Thanks.tsx @@ -19,6 +19,11 @@ const Thanks: React.FC = () => { const [err, setErr] = useState(); useEffect(() => { + const localSubscriberIdHash = localStorage.getItem('localSubscriberIdHash'); + if(localSubscriberIdHash){ + navigate('/connect-wallet'); + } + const queryParams = new URLSearchParams(location.search); const token = queryParams.get('jwt_token'); @@ -38,14 +43,12 @@ const Thanks: React.FC = () => { throw new Error("Token has expired"); } - const subscriberIdBytes = ethers.utils.toUtf8Bytes(decoded.subscriber_id) + const subscriberIdBytes = ethers.utils.toUtf8Bytes(decoded.subscriber_id); const subscriberIdHash = ethers.utils.sha256(subscriberIdBytes); - navigate('/connect-wallet', { - state:{ - subscriberIdHash - } - }); + localStorage.setItem('subscriberIdHash', subscriberIdHash); + + navigate('/connect-wallet'); } catch (error) { setErr(String(error)); } diff --git a/src/pages/UserVerification.tsx b/src/pages/UserVerification.tsx index 31ffac7..4354c3a 100644 --- a/src/pages/UserVerification.tsx +++ b/src/pages/UserVerification.tsx @@ -45,12 +45,13 @@ const UserVerification = () => { useEffect(() => { if (applicationSubmitted && kycId !== '') { const kycIdHash = ethers.utils.sha256(ethers.utils.toUtf8Bytes(kycId)); + localStorage.setItem('subscriberIdHash', kycIdHash); + navigate("/sign-with-cosmos", { state: { message, cosmosAddress, receivedEthSig, - subscriberIdHash: kycIdHash, }}) } }, [applicationSubmitted, kycId, navigate, cosmosAddress, message, receivedEthSig]);