Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 16, 2024

Listen to auth events

Amplify Auth emits events during authentication flows, which enables 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.

AWS Cognito Auth Plugin sends important events through Amplify Hub. You can listen to these events like the following:

Amplify.Hub.subscribe(HubChannel.AUTH,
hubEvent -> {
if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.name())) {
Log.i("AuthQuickstart", "Auth successfully initialized");
} else if (hubEvent.getName().equals(InitializationStatus.FAILED.name())){
Log.i("AuthQuickstart", "Auth failed to succeed");
} else {
String eventName = hubEvent.getName();
if (eventName.equals(SIGNED_IN.name())) {
Log.i("AuthQuickstart", "Auth just became signed in.");
}
else if (eventName.equals(SIGNED_OUT.name())) {
Log.i("AuthQuickstart", "Auth just became signed out.");
}
else if (eventName.equals(SESSION_EXPIRED.name())) {
Log.i("AuthQuickstart", "Auth session just expired.");
}
else if (eventName.equals(USER_DELETED.name())) {
Log.i("AuthQuickstart", "User has been deleted.");
}
else {
Log.w("AuthQuickstart", "Unhandled Auth Event: " + eventName);
}
}
}
);
Amplify.Hub.subscribe(HubChannel.AUTH) { event ->
when (event.name) {
InitializationStatus.SUCCEEDED.name ->
Log.i("AuthQuickstart", "Auth successfully initialized")
InitializationStatus.FAILED.name ->
Log.i("AuthQuickstart", "Auth failed to succeed")
else -> when (event.name) {
AuthChannelEventName.SIGNED_IN.name ->
Log.i("AuthQuickstart", "Auth just became signed in")
AuthChannelEventName.SIGNED_OUT.name ->
Log.i("AuthQuickstart", "Auth just became signed out")
AuthChannelEventName.SESSION_EXPIRED.name ->
Log.i("AuthQuickstart", "Auth session just expired")
AuthChannelEventName.USER_DELETED.name ->
Log.i("AuthQuickstart", "User has been deleted")
else ->
Log.w("AuthQuickstart", "Unhandled Auth Event: ${event.name}")
}
}
}
Amplify.Hub.subscribe(HubChannel.AUTH).collect {
when (it.name) {
InitializationStatus.SUCCEEDED.name ->
Log.i("AuthQuickstart", "Auth successfully initialized")
InitializationStatus.FAILED.name ->
Log.i("AuthQuickstart", "Auth failed to succeed")
else -> when (it.name) {
AuthChannelEventName.SIGNED_IN.name ->
Log.i("AuthQuickstart", "Auth just became signed in.")
AuthChannelEventName.SIGNED_OUT.name ->
Log.i("AuthQuickstart", "Auth just became signed out.")
AuthChannelEventName.SESSION_EXPIRED.name ->
Log.i("AuthQuickstart", "Auth session just expired.")
AuthChannelEventName.USER_DELETED.name ->
Log.i("AuthQuickstart", "User has been deleted.")
else ->
Log.w("AuthQuickstart", "Unhandled Auth Event: ${it.name}")
}
}
}
RxAmplify.Hub.on(HubChannel.AUTH)
.map(HubEvent::getName)
.subscribe(name -> {
if (name.equals(InitializationStatus.SUCCEEDED.name())) {
Log.i("AuthQuickstart", "Auth successfully initialized");
return;
} else if (name.equals(InitializationStatus.FAILED.name())) {
Log.i("AuthQuickstart", "Auth failed to succeed");
return;
} else {
if (name.equals(SIGNED_IN.name())) {
Log.i("AuthQuickstart", "Auth just became signed in.");
}
else if (name.equals(SIGNED_OUT.name())) {
Log.i("AuthQuickstart", "Auth just became signed out.");
}
else if (name.equals(SESSION_EXPIRED.name())) {
Log.i("AuthQuickstart", "Auth session just expired.");
}
else if (name.equals(USER_DELETED.name())) {
Log.i("AuthQuickstart", "User has been deleted.");
}
else {
Log.w("AuthQuickstart", "Unhandled Auth Event: " + hubEvent.getName());
}
}
});