Page updated Jan 16, 2024

Preview: AWS Amplify's new code-first DX (Gen 2)

The next generation of Amplify's backend building experience with a TypeScript-first DX.

Get started

Listen to auth events

Tapping into auth events allows you to react to user flows in real time and trigger custom business logic. For example, you may want to capture data, synchronize your app's state, and personalize the user's experience. You can listen to and respond to events across the Auth lifecycle such as sign-in and sign-out. In this guide we will describe how you can listen to these auth events.

Before you begin, you will need:

  • An Amplify project with the Auth category configured
  • The Amplify libraries installed and configured

Expose hub events triggered in response to auth actions

You can use Amplify Hub with its built in Amplify Auth events to subscribe a listener using a publish-subscribe pattern and capture events between different parts of your application. The Amplify Auth category publishes in the auth channel when auth events such as signedIn or signedOut happen independent from your app code.

You can review the Amplify Hub guide to learn more.

Channels are logical group names that help you organize dispatching and listening. However, some channels are protected and cannot be used to publish custom events, and auth is one of these channels. Sending unexpected payloads to protected channels can have undesirable side effects such as impacting authentication flows. See the Amplify Hub guide for more protected channels.

Here is a basic example of setting up a listener that logs an event emitted through the auth channel:

import { Hub } from 'aws-amplify/utils'; const listener = (data) => { console.log(data); }; Hub.listen('auth', listener);
1import { Hub } from 'aws-amplify/utils';
2
3const listener = (data) => {
4 console.log(data);
5};
6
7Hub.listen('auth', listener);

Once your app is set up to subscribe and listen to specific event types from the auth channel, the listeners will be notified asynchronously when an event occurs. This pattern allows for a one-to-many relationship where one auth event can be shared with many different listeners that have been subscribed. This lets your app react based on the event rather than proactively poll for information.

Additionally, you can set up your listener to extract data from the event payload and execute a callback that you define. For example, you might update UI elements in your app to reflect your user's authenticated state after the signedIn or signedOut events.

Listen to and log auth events

One of the most common workflows will be to log events. In this example you can see how you can listen and target specific auth events using a switch to log your own messages.

import { Hub } from 'aws-amplify/utils'; Hub.listen('auth', ({ payload }) => { switch (payload.event) { case 'signedIn': console.log('user have been signedIn successfully.'); break; case 'signedOut': console.log('user have been signedOut successfully.'); break; case 'tokenRefresh': console.log('auth tokens have been refreshed.'); break; case 'tokenRefresh_failure': console.log('failure while refreshing auth tokens.'); break; case 'signInWithRedirect': console.log('signInWithRedirect API has successfully been resolved.'); break; case 'signInWithRedirect_failure': console.log('failure while trying to resolve signInWithRedirect API.'); break; case 'customOAuthState': logger.info('custom state returned from CognitoHosted UI'); break; } });
1import { Hub } from 'aws-amplify/utils';
2
3Hub.listen('auth', ({ payload }) => {
4 switch (payload.event) {
5 case 'signedIn':
6 console.log('user have been signedIn successfully.');
7 break;
8 case 'signedOut':
9 console.log('user have been signedOut successfully.');
10 break;
11 case 'tokenRefresh':
12 console.log('auth tokens have been refreshed.');
13 break;
14 case 'tokenRefresh_failure':
15 console.log('failure while refreshing auth tokens.');
16 break;
17 case 'signInWithRedirect':
18 console.log('signInWithRedirect API has successfully been resolved.');
19 break;
20 case 'signInWithRedirect_failure':
21 console.log('failure while trying to resolve signInWithRedirect API.');
22 break;
23 case 'customOAuthState':
24 logger.info('custom state returned from CognitoHosted UI');
25 break;
26 }
27});

Stop listening to events

You can also stop listening for messages by calling the result of the Hub.listen() function. This may be useful if you no longer need to receive messages in your application flow. This can also help you avoid any memory leaks on low powered devices when you are sending large amounts of data through Amplify Hub on multiple channels.

To stop listening to a certain event, you need to wrap the listener function with a variable and call it once you no longer need it:

/* start listening for messages */ const hubListenerCancelToken = Hub.listen('auth', (data) => { console.log('Listening for all auth events: ', data.payload.data); }); /* later */ hubListenerCancelToken(); // stop listening for messages
1/* start listening for messages */
2const hubListenerCancelToken = Hub.listen('auth', (data) => {
3 console.log('Listening for all auth events: ', data.payload.data);
4});
5
6/* later */
7hubListenerCancelToken(); // stop listening for messages

You now have a few use cases and examples for listening to and responding to auth events.

Conclusion

Congratulations! You finished the Listen to auth events guide. In this guide, you learned how to listen for auth events across workflows.

Next steps

Now that you can listen to auth events you may also want to add some additional features. We recommend you learn more about: