Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mihiriroid opened this issue Aug 23, 2024 · 3 comments
Labels
question General question

Comments

@mihiriroid
Copy link

Bug Description

  • In my application, I have call functionality. I use IoT subscribe events to get the current status, and for authentication, we are using Amplify. When I try to connect to my IoT endpoint using the mqttClient.connect() method, I am able to connect. The app works fine, but when I kill my app and reopen it, I get an error message saying You are currently signed out. I didn't sign out, but still, this error occurs. If I remove the mqttClient.connect() code and follow the same steps, the app works fine. I don't understand why the user is signed out when I connect to my IoT endpoints using mqttClient.connect().

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?

  • Amplify.Auth.fetchAuthSession

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):

  • AWS Android SDK Version: [aws-android-sdk-iot ver: 2.76.1, Amplify: amplifyframework:aws-auth-cognito ver: 2.21.0]
  • Device: [Any Android Device]
  • Android Version: [Any Android version]
  • Specific to simulators: [e.g. No]
@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Aug 23, 2024
@tylerjroach
Copy link
Member

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.

@tylerjroach tylerjroach added the question General question label Aug 23, 2024
@github-actions github-actions bot removed pending-maintainer-response Issue is pending response from an Amplify team member pending-triage Issue is pending triage labels Aug 23, 2024
@tylerjroach tylerjroach added pending-triage Issue is pending triage closing soon Issue will auto-close if there is no additional activity within 7 days. and removed pending-triage Issue is pending triage labels Aug 23, 2024
@mihiriroid
Copy link
Author

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

@github-actions github-actions bot added pending-maintainer-response Issue is pending response from an Amplify team member and removed closing soon Issue will auto-close if there is no additional activity within 7 days. pending-maintainer-response Issue is pending response from an Amplify team member labels Aug 26, 2024
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

2 participants