Listen to auth events
AWS Cognito Auth Plugin sends important events through Amplify Hub.
Amplify.Hub.subscribe(HubChannel.AUTH, hubEvent -> { if (hubEvent.getName().equals(InitializationStatus.SUCCEEDED.toString())) { Log.i("AuthQuickstart", "Auth successfully initialized"); } else if (hubEvent.getName().equals(InitializationStatus.FAILED.toString())){ Log.i("AuthQuickstart", "Auth failed to succeed"); } else { switch (AuthChannelEventName.valueOf(hubEvent.getName())) { case SIGNED_IN: Log.i("AuthQuickstart", "Auth just became signed in."); break; case SIGNED_OUT: Log.i("AuthQuickstart", "Auth just became signed out."); break; case SESSION_EXPIRED: Log.i("AuthQuickstart", "Auth session just expired."); break; case USER_DELETED: Log.i("AuthQuickstart", "User has been deleted."); break; default: Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(hubEvent.getName())); break; } } });
Amplify.Hub.subscribe(HubChannel.AUTH) { event -> when (event.name) { InitializationStatus.SUCCEEDED.toString() -> Log.i("AuthQuickstart", "Auth successfully initialized") InitializationStatus.FAILED.toString() -> Log.i("AuthQuickstart", "Auth failed to succeed") else -> when (AuthChannelEventName.valueOf(event.name)) { AuthChannelEventName.SIGNED_IN -> Log.i("AuthQuickstart", "Auth just became signed in") AuthChannelEventName.SIGNED_OUT -> Log.i("AuthQuickstart", "Auth just became signed out") AuthChannelEventName.SESSION_EXPIRED -> Log.i("AuthQuickstart", "Auth session just expired") AuthChannelEventName.USER_DELETED -> 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.toString() -> Log.i("AuthQuickstart", "Auth successfully initialized") InitializationStatus.FAILED.toString() -> Log.i("AuthQuickstart", "Auth failed to succeed") else -> when (AuthChannelEventName.valueOf(it.name)) { AuthChannelEventName.SIGNED_IN -> Log.i("AuthQuickstart", "Auth just became signed in.") AuthChannelEventName.SIGNED_OUT -> Log.i("AuthQuickstart", "Auth just became signed out.") AuthChannelEventName.SESSION_EXPIRED -> Log.i("AuthQuickstart", "Auth session just expired.") AuthChannelEventName.USER_DELETED -> Log.i("AuthQuickstart", "User has been deleted.") } }}
RxAmplify.Hub.on(HubChannel.AUTH) .map(HubEvent::getName) .subscribe(name -> { if (name.equals(InitializationStatus.SUCCEEDED.toString())) { Log.i("AuthQuickstart", "Auth successfully initialized"); return; } else if (name.equals(InitializationStatus.FAILED.toString())) { Log.i("AuthQuickstart", "Auth failed to succeed"); return; } switch (AuthChannelEventName.valueOf(name)) { case SIGNED_IN: Log.i("AuthQuickstart", "Auth just became signed in."); break; case SIGNED_OUT: Log.i("AuthQuickstart", "Auth just became signed out."); break; case SESSION_EXPIRED: Log.i("AuthQuickstart", "Auth session just expired."); break; case USER_DELETED: Log.i("AuthQuickstart", "User has been deleted."); break; default: Log.w("AuthQuickstart", "Unhandled Auth Event: " + AuthChannelEventName.valueOf(name)); break; } });