Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add new react hooks to improve devex #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

avasisht23
Copy link
Contributor

No description provided.

@avasisht23 avasisht23 changed the base branch from main to moldy/hooks April 11, 2024 06:28
@mwawrusch
Copy link

mwawrusch commented Apr 16, 2024

Hi @avasisht23 @moldy530 , I partially implemented this but ran into a few issues:

It seems that

  useAddPasskey,
  use,
   useSignMessage,
  useExportAccount,
  useLogout,

are not exported from the current alchemy build 3.8.1 - is this correct?

Secondly, I need to sign a siwe message after successful login to obtain a backend session. What is the correct way to do this?

Thanks

@avasisht23
Copy link
Contributor Author

hey @mwawrusch that's right! I was testing on this branch the changes I'm making here in aa-sdk.

  1. feat: add useAddPasskey hook aa-sdk#565
  2. feat: add useLogout hook aa-sdk#566
    ... and so on in the pull requests I have open there

I'll be planning to add these hooks very shortly (in 1-2 days) 🤞 and we can ideally launch in 3.8.2

@mwawrusch
Copy link

Thanks. Great work btw, really appreciate the simplicity.

@avasisht23
Copy link
Contributor Author

@mwawrusch no worries! will ping you when they are live.

Secondly, I need to sign a siwe message after successful login to obtain a backend session. What is the correct way to do this?

As for this, I believe once you get the signer back after authenticating, you can connect it to a smart account client to sign a SIWE message using the useSignMessage hook? If you want to use the signer as an EOA signer, I think you can just use signMessage on the authenticated signer itself to sign the SIWE message. lemme know if that works.

@mwawrusch
Copy link

@mwawrusch no worries! will ping you when they are live.

Secondly, I need to sign a siwe message after successful login to obtain a backend session. What is the correct way to do this?

As for this, I believe once you get the signer back after authenticating, you can connect it to a smart account client to sign a SIWE message using the useSignMessage hook? If you want to use the signer as an EOA signer, I think you can just use signMessage on the authenticated signer itself to sign the SIWE message. lemme know if that works.

Thanks. The actual signing i have handled, it's more about what's the best way to get notified that a connection happened, like an onConnect callback in the AlchemyAccountProvider, or listening to the state change via useSignerStatus.

@mwawrusch
Copy link

mwawrusch commented Apr 16, 2024

Here is my current implementation. It does trigger the sign in twice on development at least (which might be the react dev model double invokation thing).
Feels like there is some potential for errors in there.

'use client'

import { AlchemySignerStatus } from "@alchemy/aa-alchemy";
import { useAccount, useSigner, useSignerStatus } from "@alchemy/aa-alchemy/react";
import { useSession } from "next-auth/react";
import { PropsWithChildren, useEffect } from "react";
import { handleBackendLogin } from "./handleBackendLogin";
import { useRouter } from "next/navigation"


export interface RojiSessionHandlerProps {
  isProduction: boolean;
}


export const RojiSessionHandler = ({children, isProduction}: PropsWithChildren<RojiSessionHandlerProps>) => {
  const signerStatus = useSignerStatus();
  const signer = useSigner()
  const { account } = useAccount({type: "MultiOwnerModularAccount",});
  const session = useSession();
  const router = useRouter();


  // Handles the scenario where the user is authenticated but the session is not.
  useEffect( () => {
    const fn = async () => {
      if(signerStatus?.status !== AlchemySignerStatus.CONNECTED) {
        return;
      }
      if(!signer) {
        return;
      }
      if(!account) {
        return;
      }

      const data = session?.data as any || {};
      const sessionStatus = session?.status;

      if(sessionStatus !== 'unauthenticated') {
        // NOTE - we need to also check that the correct user is authenticated.
        return;
      }
    
      console.log('Session status', sessionStatus);
      console.log('Session data', data);

      const authDetails = await signer.getAuthDetails();

      const optionalEmail = authDetails?.email

      const chainId = isProduction ? 8453 : 11155111;
      await handleBackendLogin({signer: signer, 
                                userAddress: await signer.getAddress(), 
                                accountAddress: account.address , 
                                chainId, 
                                email: optionalEmail})

      router.refresh();
      router.push('/pro');
      }
    
    fn();

  }, [signerStatus.status, session, account, signer, isProduction]);


  return (<>{children}</>);
}

@avasisht23 avasisht23 force-pushed the 04-11-refactor_add_new_react_hooks_to_improve_devex branch from 9c5e988 to 76d363b Compare April 16, 2024 15:35
@moldy530 moldy530 force-pushed the moldy/hooks branch 2 times, most recently from b440863 to ccd2aad Compare April 24, 2024 18:29
Base automatically changed from moldy/hooks to main April 24, 2024 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants