From f386d2933f1f70d7b9486cadee6f8a3d180ae2d2 Mon Sep 17 00:00:00 2001 From: "Vlad Lo." Date: Fri, 9 Aug 2024 17:21:42 +0300 Subject: [PATCH] Get transactions --- src/components/AuthLogin/AuthModal.tsx | 140 ++++++++++++++++--------- src/pages/developer-tools/faucet.tsx | 11 +- src/theme/Root.tsx | 1 + 3 files changed, 103 insertions(+), 49 deletions(-) diff --git a/src/components/AuthLogin/AuthModal.tsx b/src/components/AuthLogin/AuthModal.tsx index a790a29a7d..bd40ec8126 100644 --- a/src/components/AuthLogin/AuthModal.tsx +++ b/src/components/AuthLogin/AuthModal.tsx @@ -3,7 +3,14 @@ import Modal from "react-modal"; import styles from "./styles.module.css"; import global from "../ParserOpenRPC/global.module.css"; import Icon from "../Icon/Icon"; -import { authenticateAndAuthorize, AUTH_WALLET_PAIRING, AUTH_WALLET_SESSION_NAME, AUTH_WALLET_PROJECTS, saveTokenString, getUserIdFromSessionStorage } from "../../lib/siwsrp/auth"; +import { + authenticateAndAuthorize, + AUTH_WALLET_PAIRING, + AUTH_WALLET_SESSION_NAME, + AUTH_WALLET_PROJECTS, + saveTokenString, + getUserIdFromSessionStorage, +} from "../../lib/siwsrp/auth"; import { DASHBOARD_URL, REQUEST_PARAMS } from "@site/src/lib/constants"; Modal.setAppElement("#__docusaurus"); @@ -11,6 +18,7 @@ type AuthModalProps = { open: boolean; setOpen: (arg: boolean) => void; setProjects: (arg: any[]) => void; + setUser?: (arg: string) => void; }; enum AUTH_LOGIN_STEP { @@ -50,26 +58,46 @@ const ConnectingModal = () => { ); }; -const ConnectionErrorModal = ({onClose, onRetry}: {onClose: VoidFunction, onRetry: VoidFunction}) => { +const ConnectionErrorModal = ({ + onClose, + onRetry, +}: { + onClose: VoidFunction; + onRetry: VoidFunction; +}) => { return ( <>
-
There was an issue connecting your wallet
+
+ There was an issue connecting your wallet +
Please try again or contact us.
- - + +
); }; -const AuthModal = ({ open, setOpen, setProjects }: AuthModalProps) => { +const AuthModal = ({ open, setOpen, setProjects, setUser }: AuthModalProps) => { const [step, setStep] = useState(AUTH_LOGIN_STEP.CONNECTING); const login = async () => { @@ -90,55 +118,71 @@ const AuthModal = ({ open, setOpen, setProjects }: AuthModalProps) => { }); const usersPairing = await pairingResponse.json(); - const { data } = usersPairing - // Saving of paired Infura accounts in local storage - localStorage.setItem(AUTH_WALLET_PAIRING, JSON.stringify({ data })) - + const { data } = usersPairing; + // Saving of paired Infura accounts in local storage + localStorage.setItem(AUTH_WALLET_PAIRING, JSON.stringify({ data })); + // Handling no wallet pairing or multiple pairing if (data.length !== 1) { - const mm_auth = Buffer.from(JSON.stringify({ - token: true, - step: data.length > 1 ? AUTH_LOGIN_STEP.WALLET_LOGIN_MULTI_USER : AUTH_LOGIN_STEP.WALLET_LOGIN_EMAIL_PASSWORD, - mmAuthSession: localStorage.getItem(AUTH_WALLET_SESSION_NAME), - walletPairing: data - })).toString('base64') - window.location.href = `${DASHBOARD_URL}/login?mm_auth=${mm_auth}&token=true&redirect_to=${window.location.href}` - return + const mm_auth = Buffer.from( + JSON.stringify({ + token: true, + step: + data.length > 1 + ? AUTH_LOGIN_STEP.WALLET_LOGIN_MULTI_USER + : AUTH_LOGIN_STEP.WALLET_LOGIN_EMAIL_PASSWORD, + mmAuthSession: localStorage.getItem(AUTH_WALLET_SESSION_NAME), + walletPairing: data, + }), + ).toString("base64"); + window.location.href = `${DASHBOARD_URL}/login?mm_auth=${mm_auth}&token=true&redirect_to=${window.location.href}`; + return; } - + // We have one wallet paired with one Infura account // Use this Infura email account and this ProfileId to login to Infura // Pass token in request params to generate and recieve an Infura access Token - const email = data[0].email as string - const userWithTokenResponse = await fetch(`${DASHBOARD_URL}/api/wallet/login?token=true`, { - ...REQUEST_PARAMS(), - headers: { - ...REQUEST_PARAMS().headers, - hydra_token: accessToken, - recaptcha_bypass: "84450394", + const email = data[0].email as string; + const userWithTokenResponse = await fetch( + `${DASHBOARD_URL}/api/wallet/login?token=true`, + { + ...REQUEST_PARAMS(), + headers: { + ...REQUEST_PARAMS().headers, + hydra_token: accessToken, + recaptcha_bypass: "84450394", + }, + body: JSON.stringify({ + email, + profileId: userProfile.profileId, + }), }, - body: JSON.stringify({ - email, - profileId: userProfile.profileId - }) - }); + ); const { token } = await userWithTokenResponse.json(); - saveTokenString(token) - const userId = getUserIdFromSessionStorage() + saveTokenString(token); + const userId = getUserIdFromSessionStorage(); // You can use Infura Access Token to fetch any Infura API endpoint - const projectsResponse = await fetch(`${DASHBOARD_URL}/api/v1/users/${userId}/projects`, { - ...REQUEST_PARAMS('GET'), - headers: { - ...REQUEST_PARAMS('GET').headers, - Authorization: `Bearer ${token}` - } - }); - const { result: { projects }} = await projectsResponse.json() - sessionStorage.setItem(AUTH_WALLET_PROJECTS, JSON.stringify(projects)) - setProjects(projects) - setOpen(false) + const projectsResponse = await fetch( + `${DASHBOARD_URL}/api/v1/users/${userId}/projects`, + { + ...REQUEST_PARAMS("GET"), + headers: { + ...REQUEST_PARAMS("GET").headers, + Authorization: `Bearer ${token}`, + }, + }, + ); + const { + result: { projects }, + } = await projectsResponse.json(); + sessionStorage.setItem(AUTH_WALLET_PROJECTS, JSON.stringify(projects)); + setProjects(projects); + if (setUser) { + setUser(userId); + } + setOpen(false); }; useEffect(() => { @@ -150,13 +194,13 @@ const AuthModal = ({ open, setOpen, setProjects }: AuthModalProps) => { const handleLogin = () => { (async () => { try { - setStep(AUTH_LOGIN_STEP.CONNECTING) + setStep(AUTH_LOGIN_STEP.CONNECTING); await login(); } catch (e: any) { - setStep(AUTH_LOGIN_STEP.CONNECTION_ERROR) + setStep(AUTH_LOGIN_STEP.CONNECTION_ERROR); } - })() - } + })(); + }; return ( { + const transactions = await fetch( + `${DASHBOARD_URL}/api/v1/faucets/linea/transactions?take=10&skip=0`, + GET_OPTIONS, + ); + console.log(transactions) + }; + useEffect(() => { if (userId) { - setTransactions; + getTransactions(); } }, [userId]); diff --git a/src/theme/Root.tsx b/src/theme/Root.tsx index 60ebf61a8c..7ccff719a8 100644 --- a/src/theme/Root.tsx +++ b/src/theme/Root.tsx @@ -192,6 +192,7 @@ export const LoginProvider = ({ children }) => { open={openAuthModal} setOpen={setOpenAuthModal} setProjects={setProjects} + setUser={setUserId} /> );