Search code examples
androidamazon-web-servicesamazon-cognitoaws-amplifyaws-userpools

AWS auth seesion not keeping alive when we close the android application


I'm using AWS Amplify SDK for Cognito integration. Actually, I successfully integrated user sign in and sign up flows based on the documentation available here. However, after the successful login if we restart the app the user session is not persisting. Amplify.Auth.fetchAuthSession() always returns isSignedIn as false. I have no idea where I'm making a problem. Please find the 'sign in' snippet below.

fun onSingIn(view: View) {
    Amplify.Auth.signIn(
            email.value,
            password.value,
            { result ->
                Log.i(TAG, if (result.isSignInComplete) "Sign in succeeded" else "Sign in not complete")

                if (result.isSignInComplete) {
                    view.context.let {
                        it.startActivity(Intent(it, FeedActivity::class.java))
                        signInStatus.postValue(true)
                    }
                }

            },
            { error ->
                view.context.let {
                    var message = it.getString(R.string.something_went_wrong)
                    when (error.cause) {
                        is UserNotConfirmedException ->{
                            email.value?.let {emailAddress ->
                                val direction =LoginFragmentDirections
                                        .actionLoginFragmentToEmailVerificationCodeFragment(emailAddress)
                                view.findNavController().navigate(direction)
                            }
                        }
                        is UserNotFoundException ->
                            message = it.getString(R.string.credentilas_incorrect)
                        else->
                            viewModelScope.launch(Dispatchers.Main) { it.showToast(message) }
                    }
                    Log.e(TAG, error.toString())
                }

            }
    )
}

Solution

  • Finally resolved the issue. The problem was related user pool app client settings configurations. I haven't checked identity providers inside app client settings. After choosing 'Cognito User Pool' as an identity provider the login session stayed alive without any issues. Hope this answer will help someone facing a similar issue.