farcasterxyz / auth-monorepo

Farcaster Auth monorepo
MIT License
41 stars 29 forks source link

Having issue with useSignIn hook with NextJS #162

Open jsonpreet opened 4 months ago

jsonpreet commented 4 months ago

What is the bug? I am trying useSignIn hook in NextJS 14.1.4, I logged In from Wrapcast app after Scanning QR code, but in return onSuccess event is not working. In console i am getting two things 1 is from farcaster relay and 2nd posting to blockchain.

image

How can it be reproduced? (optional) https://github.com/jsonpreet/farcaster-login

valentinludu commented 2 months ago

Had the exact same issue today. I managed to make it work by wrapping the methods in the useCallback hook. For some reason the onSuccess/onError methods are not being triggered if they are not cached.

const onSuccessCallback = useCallback( (res: StatusAPIResponse) => { // Do something }, []);

const onErrorCallback = useCallback( (error?: AuthClientError) => { // Do something }, []);

const signInState = useSignIn({ onSuccess: onSuccessCallback, onError: onErrorCallback, });

AmAzing129 commented 2 months ago

You might not be using 'useCallback' the way you intend to.

const onError = (error: any) => {
  console.log('error', error)
}

const onErrorCallback = useCallback(
  (error?: AuthClientError) => {
  onError?.(error)
}, [onError])

Considering onError will change every render, onErrorCallback will change too.

It's the same as onError: (error: any) => console.log('error', error)

In useSignIn, all the callback functions are dependencies. So it may cause the problem.

Maybe you can use the way the answer above mentioned to make the function unchanged during every rendering. It should be unchanged after all.