From 3549cf47fb2ddfd68577f1504e4694d665e9e326 Mon Sep 17 00:00:00 2001 From: Magne Cedric Date: Tue, 24 Sep 2024 14:37:30 +0200 Subject: [PATCH 1/3] feat(mm-login): improve metamask login --- src/components/AuthLogin/AuthModal.tsx | 41 +++++++++++++++++--------- src/components/ParserOpenRPC/index.tsx | 8 ++--- src/lib/constants.js | 6 ++-- src/lib/siwsrp/auth.ts | 19 ++++++------ 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/components/AuthLogin/AuthModal.tsx b/src/components/AuthLogin/AuthModal.tsx index d770dbf961b..446b751d451 100644 --- a/src/components/AuthLogin/AuthModal.tsx +++ b/src/components/AuthLogin/AuthModal.tsx @@ -1,6 +1,7 @@ import React, { useContext, useEffect } from "react"; import Modal from "react-modal"; import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; +import { useLocation } from "@docusaurus/router"; import styles from "./styles.module.css"; import global from "../ParserOpenRPC/global.module.css"; import Icon from "../Icon/Icon"; @@ -11,7 +12,7 @@ import { saveTokenString, getUserIdFromJwtToken, } from "../../lib/siwsrp/auth"; -import { DASHBOARD_URL, REQUEST_PARAMS } from "@site/src/lib/constants"; +import { DASHBOARD_URL, REQUEST_PARAMS, REF_PATH } from "@site/src/lib/constants"; import { MetamaskProviderContext } from "@site/src/theme/Root"; Modal.setAppElement("#__docusaurus"); @@ -130,15 +131,20 @@ const ConnectionErrorModal = ({ ); }; -const AuthModal = ({ - open, - setOpen, - step, - setStep, -}: AuthModalProps) => { +const AuthModal = ({ open, setOpen, step, setStep }: AuthModalProps) => { const { siteConfig } = useDocusaurusContext(); const { DASHBOARD_PREVIEW_URL, VERCEL_ENV } = siteConfig?.customFields || {}; - const { sdk, setWalletLinked, setWalletLinkUrl, metaMaskDisconnect, setProjects, setMetaMaskAccount, setMetaMaskProvider } = useContext(MetamaskProviderContext); + const { + sdk, + setWalletLinked, + setWalletLinkUrl, + metaMaskDisconnect, + setProjects, + setMetaMaskAccount, + setMetaMaskProvider, + } = useContext(MetamaskProviderContext); + const location = useLocation(); + const { pathname } = location; const login = async () => { setStep(AUTH_LOGIN_STEP.CONNECTING); @@ -155,10 +161,12 @@ const AuthModal = ({ const provider = sdk.getProvider(); setMetaMaskProvider(provider); } - + // Call Profile SDK API to retrieve Hydra Access Token & Wallet userProfile // Hydra Access Token will be used to fetch Infura API - const { accessToken, userProfile } = await authenticateAndAuthorize(VERCEL_ENV as string); + const { accessToken, userProfile } = await authenticateAndAuthorize( + VERCEL_ENV as string + ); const loginResponse = await ( await fetch( @@ -168,7 +176,7 @@ const AuthModal = ({ headers: { ...REQUEST_PARAMS().headers, hydra_token: accessToken, - token: 'true', + token: "true", }, body: JSON.stringify({ profileId: userProfile.profileId, @@ -189,7 +197,7 @@ const AuthModal = ({ step: data.step, mmAuthSession: localStorage.getItem(AUTH_WALLET_SESSION_NAME), walletPairing: data.pairing, - token: true + token: true, }) ).toString("base64"); @@ -239,8 +247,13 @@ const AuthModal = ({ setProjects(projects); setOpen(false); } catch (e: any) { - setStep(AUTH_LOGIN_STEP.CONNECTION_ERROR); - setOpen(true); + if (pathname.startsWith('/wallet/reference')) { + setStep(AUTH_LOGIN_STEP.CONNECTION_SUCCESS); + setOpen(true); + } else { + setStep(AUTH_LOGIN_STEP.CONNECTION_ERROR); + setOpen(true); + } } }; diff --git a/src/components/ParserOpenRPC/index.tsx b/src/components/ParserOpenRPC/index.tsx index c1cd54c390b..ee00b0d5f43 100644 --- a/src/components/ParserOpenRPC/index.tsx +++ b/src/components/ParserOpenRPC/index.tsx @@ -18,6 +18,7 @@ import { import { AuthBox } from "@site/src/components/ParserOpenRPC/AuthBox"; import { MetamaskProviderContext } from "@site/src/theme/Root"; import ProjectsBox from "@site/src/components/ParserOpenRPC/ProjectsBox"; +import { REF_PATH } from "@site/src/lib/constants"; interface ParserProps { network: NETWORK_NAMES; @@ -36,8 +37,6 @@ interface ParserOpenRPCContextProps { export const ParserOpenRPCContext = createContext(null); -const REF_PATH = "/wallet/reference/new-reference"; - export default function ParserOpenRPC({ network, method, @@ -52,8 +51,9 @@ export default function ParserOpenRPC({ const [isDrawerContentFixed, setIsDrawerContentFixed] = useState(false); const [drawerLabel, setDrawerLabel] = useState(null); const [isComplexTypeView, setIsComplexTypeView] = useState(false); - const { metaMaskAccount, metaMaskProvider } = - useContext(MetamaskProviderContext); + const { metaMaskAccount, metaMaskProvider } = useContext( + MetamaskProviderContext + ); const { colorMode } = useColorMode(); const openModal = () => { setModalOpen(true); diff --git a/src/lib/constants.js b/src/lib/constants.js index 7458877b6dc..f70964657b9 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -6,9 +6,9 @@ export const DASHBOARD_URL = (DASHBOARD_PREVIEW_URL, VERCEL_ENV) => DASHBOARD_PR ? DASHBOARD_PREVIEW_URL : VERCEL_ENV === "production" ? PROD_APP_URL - : VERCEL_ENV === "preview" - ? STAGE_APP_URL - : DEV_APP_URL; + : STAGE_APP_URL; + +export const REF_PATH = "/wallet/reference/new-reference"; const TEST_TRANSACTIONS = { mainnet: { diff --git a/src/lib/siwsrp/auth.ts b/src/lib/siwsrp/auth.ts index 416312f3565..586d4f57795 100644 --- a/src/lib/siwsrp/auth.ts +++ b/src/lib/siwsrp/auth.ts @@ -10,7 +10,7 @@ type HydraEnv = { }; const { AuthType, Env, getEnvUrls, JwtBearerAuth, Platform } = SDK; -let VERCEL_ENV = 'development' +let VERCEL_ENV: string export const AUTH_WALLET_SESSION_NAME = "auth.wallet.session"; export const AUTH_WALLET_TOKEN = "auth.wallet.token"; export const AUTH_WALLET_PROJECTS = "auth.wallet.projects"; @@ -18,19 +18,19 @@ export const AUTH_WALLET_PROJECTS = "auth.wallet.projects"; export const getHydraEnv = (): HydraEnv => { const platform = Platform.INFURA; - // if (VERCEL_ENV === "production") { + if (VERCEL_ENV === "production") { return { ...getEnvUrls(Env.PRD), env: Env.PRD, platform, }; - // } else { - // return { - // ...getEnvUrls(Env.DEV), - // env: Env.DEV, - // platform, - // }; - // } + } else { + return { + ...getEnvUrls(Env.DEV), + env: Env.DEV, + platform, + }; + } }; const storage: SDK.AuthStorageOptions = { @@ -57,6 +57,7 @@ export const auth = new JwtBearerAuth( export const authenticateAndAuthorize = async (env: string) => { let accessToken: string, userProfile: SDK.UserProfile; VERCEL_ENV = env; + console.log(VERCEL_ENV) try { await auth.connectSnap(); accessToken = await auth.getAccessToken(); From 7391b0609864011e4bbed039c870f6034b40c603 Mon Sep 17 00:00:00 2001 From: Magne Cedric Date: Tue, 24 Sep 2024 14:40:27 +0200 Subject: [PATCH 2/3] feat(mm-login): remove unused metamask connect handler --- src/components/ParserOpenRPC/AuthBox/index.tsx | 4 ++-- src/theme/Root.tsx | 13 ------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/components/ParserOpenRPC/AuthBox/index.tsx b/src/components/ParserOpenRPC/AuthBox/index.tsx index 2d3facaf769..d5a1b634751 100644 --- a/src/components/ParserOpenRPC/AuthBox/index.tsx +++ b/src/components/ParserOpenRPC/AuthBox/index.tsx @@ -10,14 +10,14 @@ interface AuthBoxProps { } export const AuthBox = ({ isMetamaskNetwork = false }: AuthBoxProps) => { - const { metaMaskConnectHandler, metaMaskWalletIdConnectHandler } = useContext(MetamaskProviderContext); + const { metaMaskWalletIdConnectHandler } = useContext(MetamaskProviderContext); const connectHandler = () => { trackClickForSegment({ eventName: "Connect wallet", clickType: "Connect wallet", userExperience: "B", }); - isMetamaskNetwork ? metaMaskConnectHandler() : metaMaskWalletIdConnectHandler(); + metaMaskWalletIdConnectHandler(); }; return (
diff --git a/src/theme/Root.tsx b/src/theme/Root.tsx index f8ece80b667..ffc0cf92dee 100644 --- a/src/theme/Root.tsx +++ b/src/theme/Root.tsx @@ -44,7 +44,6 @@ interface Project { interface IMetamaskProviderContext { projects: { [key: string]: Project }; setProjects: (arg: { [key: string]: Project }) => void; - metaMaskConnectHandler: () => Promise; metaMaskDisconnect: () => Promise; metaMaskWalletIdConnectHandler: () => Promise; userId: string | undefined; @@ -63,7 +62,6 @@ interface IMetamaskProviderContext { export const MetamaskProviderContext = createContext({ projects: {}, setProjects: () => {}, - metaMaskConnectHandler: () => new Promise(() => {}), metaMaskDisconnect: () => new Promise(() => {}), metaMaskWalletIdConnectHandler: () => new Promise(() => {}), userId: undefined, @@ -125,16 +123,6 @@ export const LoginProvider = ({ children }) => { } catch (e) {} }; - const metaMaskConnectHandler = async () => { - try { - const accounts = await sdk.connect(); - setMetaMaskAccount(accounts); - if (accounts && accounts.length > 0) { - setMetaMaskAccount(accounts[0]); - } - } catch (e) {} - }; - useEffect(() => { const provider = sdk?.getProvider(); setMetaMaskProvider(provider); @@ -224,7 +212,6 @@ export const LoginProvider = ({ children }) => { setMetaMaskAccount, projects, setProjects, - metaMaskConnectHandler, metaMaskDisconnect, metaMaskWalletIdConnectHandler, userId, From dbf57b506be0e311c5ca256740848332918a401f Mon Sep 17 00:00:00 2001 From: Magne Cedric Date: Wed, 25 Sep 2024 15:58:47 +0200 Subject: [PATCH 3/3] feat(unified-login): removing console.log --- src/lib/siwsrp/auth.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/siwsrp/auth.ts b/src/lib/siwsrp/auth.ts index 586d4f57795..d4538607a0f 100644 --- a/src/lib/siwsrp/auth.ts +++ b/src/lib/siwsrp/auth.ts @@ -57,7 +57,6 @@ export const auth = new JwtBearerAuth( export const authenticateAndAuthorize = async (env: string) => { let accessToken: string, userProfile: SDK.UserProfile; VERCEL_ENV = env; - console.log(VERCEL_ENV) try { await auth.connectSnap(); accessToken = await auth.getAccessToken();