aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 549 forks source link

Current user session expires after connecting to the IoT EndPoint using mqttClient.connect(). #3631

Closed mihiriroid closed 2 weeks ago

mihiriroid commented 3 weeks ago

Bug Description

To Reproduce Steps:

  1. Sign in using Amplify.
  2. Retrieve the authentication session and attributes on the Home screen.
  3. Attempt to connect to IoT endpoints using mqttClient.connect().
  4. After a successful connection, attempt to retrieve the authentication session data using Amplify.Auth.fetchAuthSession - it should work fine, and you will find authSession.isSignedIn to be true.
  5. The app functions properly while in the foreground. However, if the app is killed and removed from the background,
  6. When reopening the app, attempt to retrieve the auth session data using Amplify.Auth.fetchAuthSession will result in authSession.isSignedIn being false.

Code for fetching AuthSession

  try {
            Amplify.Auth.fetchAuthSession({ authSession ->
                Logger.e("authSession: ${authSession.isSignedIn}")
            }
    , { authException ->
        Logger.e("authException: ${authException.printStackTrace()}")
    })
} catch (e: Exception) {
    Logger.e("Exception: ${e.printStackTrace()}")
    e.printStackTrace()
}

Code for connect to IoT Endpoints and subscribe topic.

 private fun connectAndSubscribeToEvents(
        clientId: String,
        credentialsProvider: CognitoCachingCredentialsProvider,
        accountId: String,
    ) {

val credentialsProvider = CognitoCachingCredentialsProvider(
                    context, // Application context
                    BuildConfig.COG_IDENTITY_POOL_ID, // Identity pool ID
                    Regions.US_EAST_1 // Region
                )

        mqttClient = AWSIotMqttManager.from(Region.getRegion(Regions.US_EAST_1),
            AWSIotMqttManager.ClientId.fromString(clientId),
            AWSIotMqttManager.Endpoint.fromString("XXXXXXXXX-ats.iot.us-east-1.amazonaws.com"))

        mqttClient?.disconnect()
        mqttClient?.setCleanSession(false)

        mqttClient?.connect(credentialsProvider) { status, throwable ->
            when (status) {
                AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting -> {
                    Logger.d("IoT: Connecting")
                }

                AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected -> {
                    Logger.d("IoT: Connected")
                    ioTConnectionCallback?.onIoTEventConnected()

                    // Subscribe to a topic
                    mqttClient?.subscribeToTopic(
                        "taygo/${accountId}/call/status",
                        AWSIotMqttQos.QOS0
                    ) { topic, data ->
                        Logger.d("IoT Message received from topic: $topic - ${String(data)}")
                        val escapedJsonString = String(data)
                        manageIotPayload(escapedJsonString)
                    }
                }

                AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Reconnecting -> {
                    Logger.d("IoT: Reconnecting: ${throwable?.message}")
                    ioTConnectionCallback?.onIoTEventConnectionFailed()
                }

                AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.ConnectionLost -> {
                    Logger.d("IoT: ConnectionLost: ${throwable?.message}")
                    ioTConnectionCallback?.onIoTEventConnectionFailed()
                }

                else -> {
                    Logger.d("IoT: Disconnected: ${throwable?.message}")
                    ioTConnectionCallback?.onIoTEventConnectionFailed()
                }
            }
        }
    }

Which AWS service(s) are affected?

Expected behavior If I don't sign out, I should stay signed in until the token expires or I manually sign out.

Environment Information (please complete the following information):

tylerjroach commented 3 weeks ago

Hi @mihiriroid, please see this page: https://docs.amplify.aws/gen1/android/sdk/configuration/amplify-compatibility/

Because you are using Amplify v2, you will need to implement your own credentials provider (example provided on the page) for IoT. AWS Android SDK's providers interfere with Amplify v2 and wipe its credentials on the device.

mihiriroid commented 2 weeks ago

Thanks for the help @tylerjroach, It's work for me.

github-actions[bot] commented 2 weeks ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.