From 1bf1addf0079f2b4e9c690b24a119d05864cfdfd Mon Sep 17 00:00:00 2001 From: Yoginth Date: Fri, 13 Dec 2024 20:21:51 +0530 Subject: [PATCH] Migrate to Lens v3 --- .../src/components/Shared/SwitchAccounts.tsx | 54 ++++++------------- .../mutations/auth/SwitchAccount.graphql | 9 ++++ packages/indexer/generated.ts | 34 ++++++++++++ 3 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 packages/indexer/documents/mutations/auth/SwitchAccount.graphql diff --git a/apps/web/src/components/Shared/SwitchAccounts.tsx b/apps/web/src/components/Shared/SwitchAccounts.tsx index 6d890013ef8d..6959947404af 100644 --- a/apps/web/src/components/Shared/SwitchAccounts.tsx +++ b/apps/web/src/components/Shared/SwitchAccounts.tsx @@ -6,8 +6,7 @@ import getAvatar from "@hey/helpers/getAvatar"; import { type Account, useAccountsAvailableQuery, - useAuthenticateMutation, - useChallengeMutation + useSwitchAccountMutation } from "@hey/indexer"; import { ErrorMessage, H4, Image, Spinner } from "@hey/ui"; import cn from "@hey/ui/cn"; @@ -17,7 +16,7 @@ import { useState } from "react"; import toast from "react-hot-toast"; import { useAccountStore } from "src/store/persisted/useAccountStore"; import { signIn, signOut } from "src/store/persisted/useAuthStore"; -import { useAccount, useSignMessage } from "wagmi"; +import { useAccount } from "wagmi"; import WalletSelector from "./Auth/WalletSelector"; import Loader from "./Loader"; @@ -25,7 +24,7 @@ const SwitchAccounts: FC = () => { const { reload } = useRouter(); const { currentAccount } = useAccountStore(); const [isLoading, setIsLoading] = useState(false); - const [loggingInProfileId, setLoggingInProfileId] = useState( + const [loggingInAccountId, setLoggingInAccountId] = useState( null ); const { address } = useAccount(); @@ -35,52 +34,31 @@ const SwitchAccounts: FC = () => { errorToast(error); }; - const { signMessageAsync } = useSignMessage({ mutation: { onError } }); - const { data, error, loading } = useAccountsAvailableQuery({ variables: { lastLoggedInAccountRequest: { address }, accountsAvailableRequest: { managedBy: address } } }); - const [loadChallenge] = useChallengeMutation(); - const [authenticate] = useAuthenticateMutation(); + const [switchAccount] = useSwitchAccountMutation(); if (loading) { - return ; + return ; } const accountsAvailable = data?.accountsAvailable.items || []; - const handleSwitchProfile = async (account: string) => { + const handleSwitchAccount = async (account: string) => { try { - setLoggingInProfileId(account); + setLoggingInAccountId(account); setIsLoading(true); - // Get challenge - const challenge = await loadChallenge({ - variables: { - request: { accountOwner: { account, owner: address } } - } - }); - - if (!challenge?.data?.challenge?.text) { - return toast.error(Errors.SomethingWentWrong); - } - - // Get signature - const signature = await signMessageAsync({ - message: challenge?.data?.challenge?.text - }); - // Auth profile and set cookies - const auth = await authenticate({ - variables: { request: { id: challenge.data.challenge.id, signature } } - }); + const auth = await switchAccount({ variables: { request: { account } } }); - if (auth.data?.authenticate.__typename === "AuthenticationTokens") { - const accessToken = auth.data?.authenticate.accessToken; - const refreshToken = auth.data?.authenticate.refreshToken; - const idToken = auth.data?.authenticate.idToken; + if (auth.data?.switchAccount.__typename === "AuthenticationTokens") { + const accessToken = auth.data?.switchAccount.accessToken; + const refreshToken = auth.data?.switchAccount.refreshToken; + const idToken = auth.data?.switchAccount.idToken; signOut(); signIn({ accessToken, idToken, refreshToken }); return reload(); @@ -112,15 +90,15 @@ const SwitchAccounts: FC = () => { {accountsAvailable.map((accountAvailable, index) => (