Search code examples
javascriptreactjsamazon-web-servicesamazon-cognitoaws-amplify

How to listen to auth state changed in aws cognito?


I am using aws cognito for authentication solution in my react app. I am instead of using the components provided by aws react package, I am using apis in the aws javascript library. I want to listen to the auth state changed.

After googling and going over aws docs I still can't find any such method.

How can I listen to changes in the auth state?


Solution

  • Amplify provides a utility object called Hub that can listen to auth state changes.

    See sample documentation here: https://docs.amplify.aws/lib/auth/auth-events/q/platform/js

    The way I use it is by setting it up in a custom hook that gets injected into the app.tsx file.