1 year ago

#256771

test-img

alx

Amplify `tokenRefresh` event triggered multiple times

I'm using the following code to monitor the auth process with Amplify+Cognito:

const handleHubEvent = useCallback((capsule) => {
  const event = capsule?.payload?.event;

  try {
    switch(event){
      case 'signIn_start':
      case 'signIn':
      case 'tokenRefresh':
        const user = await auth.currentAuthenticatedUser();
        setCurrentUser(user);
        break;
      case 'signOut':
      case 'tokenRefresh_failure':
        setCurrentUser(null);
        break;
    }
  } catch (e) {
    console.log("Error: ", e);
    setCurrentUser(null);
  }
}, []);

useEffect(() => {
  Hub.listen('auth', handleHubEvent);
  Hub.dispatch('auth', { event: 'signIn_start' });

  return () => Hub.remove('auth', handleHubEvent);
}, []);

The problem is that after a few hours (I guess once the session expires), the event callback handleHubEvent gets invoked continuously, around 7 times a second. I can see in the network tab multiple (successful) requests to cognito-idp.ap-southeast-2.amazonaws.com.

I can also confirm Hub.listen is only executed once, so there shouldn't be multiple event listeners for the same event.

Even after refreshing the page, the behaviour is the same, the only way to get rid of it is by deleting the cookies and sign in again.

Is there anything obviously wrong I'm doing here?

Thanks.

reactjs

aws-amplify

amplifyjs

0 Answers

Your Answer

Accepted video resources