diff --git a/packages/messenger-widget/src/components/SignIn/SignIn.tsx b/packages/messenger-widget/src/components/SignIn/SignIn.tsx index b9c8a67e5..b5b9f6bf8 100644 --- a/packages/messenger-widget/src/components/SignIn/SignIn.tsx +++ b/packages/messenger-widget/src/components/SignIn/SignIn.tsx @@ -1,7 +1,7 @@ /* eslint-disable max-len */ /* eslint-disable no-console */ import { useConnectModal } from '@rainbow-me/rainbowkit'; -import { useContext } from 'react'; +import { useContext, useEffect, useState } from 'react'; import { useAccount } from 'wagmi'; import { signInImage } from '../../assets/base64/home-image'; import { AuthContext } from '../../context/AuthContext'; @@ -13,6 +13,16 @@ import './SignIn.css'; import { changeSignInButtonStyle } from './bl'; export function SignIn() { + + // if `?sign-in-directly=true`, or for short `?sid=1`, is present in the URL, + // the user will be redirected to the wallet connection modal to connect the wallet and sign in directly + // especially useful when redirecting from the landing page + const signInDirectly = new URLSearchParams(location.search).get("sign-in-directly")?.toLowerCase() === "true" + || new URLSearchParams(location.search).get("sid") === "1"; + + const [shouldSignInDirectly] = + useState(signInDirectly); + const { isConnected } = useAccount(); const { cleanSignIn, isLoading } = useContext(AuthContext); @@ -24,6 +34,7 @@ export function SignIn() { const handleConnectWithWallet = () => { openConnectionModal(); + cleanSignIn(); }; const handleSignIn = async () => { @@ -40,6 +51,13 @@ export function SignIn() { openConnectModal && openConnectModal(); }; + // useEffect is needed to give some time to the components to be fully rendered first to avoid exceptions inside `changeSignInButtonStyle` function. + useEffect(() => { + if (shouldSignInDirectly) { + handleConnectWithWallet(); + } + }, [shouldSignInDirectly]); + return (
@@ -80,14 +98,14 @@ export function SignIn() { {!isConnected ? ( ) : ( { const [isLoading, setIsLoading] = useState(false); const [hasError, setHasError] = useState(false); + const [cleanSignInRequested, setCleanSignInRequested] = + useState(false); + //The account name that should be displayed to the user. Takes alias profiles and Crosschain names into account const [displayName, setDisplayName] = useState( undefined, @@ -86,27 +89,37 @@ export const useAuth = () => { }; //The normal sign in function that is used when the user signs in with their own account, using a web3 provider like wallet connect or metamask const cleanSignIn = async () => { - setIsLoading(true); - setHasError(false); - //Fetch the Account either from onchain or via CCIP - const account = await AccountConnector( - walletClient!, - mainnetProvider, - dm3Configuration.addressEnsSubdomain, - ).connect(address!); - - if (!account) { - throw Error('error fetching dm3Account'); - } - - await _login( - account.ensName, - address!, - account.userProfile, - (message: string) => walletClient!.signMessage({ message }), - ); + setCleanSignInRequested(true); }; + // useEffect is needed to give some time to the wallet connect variables to be initialized. + // without utilizing it the variable `walletClient` would be undefined. + useEffect(() => { + (async () => { + if (cleanSignInRequested && walletClient) { + setIsLoading(true); + setHasError(false); + //Fetch the Account either from onchain or via CCIP + const account = await AccountConnector( + walletClient!, + mainnetProvider, + dm3Configuration.addressEnsSubdomain, + ).connect(address!); + + if (!account) { + throw Error('error fetching dm3Account'); + } + + await _login( + account.ensName, + address!, + account.userProfile, + (message: string) => walletClient!.signMessage({ message }), + ); + } + })(); + }, [cleanSignInRequested, walletClient]); + //Siwe signin is used when a siwe message has been provided by an app using dm3 as a widget. //The user is signed in with a random account based on the provided secret const siweSignIn = async () => {