Alejandro Giraudo
Alejandro Giraudo

Reputation: 1

Android: How to check if the user is logged in to the Play Store

Using Kotlin, I want to verify that the user is logged in to the PlayStore before sending it to update the app.

I'm using credential manager because the other options I could find are deprecated.

Credential manager works fine when I use it to log into my app, but it doesn't give me information about whether user is logged into the PlayStore.

NoCredentialException is always thrown if I don't add the credentials inside my app.

private suspend fun checkGoogleAccount(context: Context) {
    val credentialManager = CredentialManager.create(context)
    try {

        val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
            .setFilterByAuthorizedAccounts(false)
            .setAutoSelectEnabled(false)
            .setServerClientId(getString(R.string.web_client_id))
            .build()

        val request: GetCredentialRequest = GetCredentialRequest.Builder()
            .addCredentialOption(googleIdOption)
            .build()

        val credentialResponse = credentialManager.getCredential(
            context = context,
            request = request
        )
        android.util.Log.e("TAG","credentialResponse ${credentialResponse.credential}")
    } catch (e: GetCredentialCancellationException) {
        android.util.Log.e("TAG", "GetCredentialCancellationException ${e.message}")
    } catch (e: NoCredentialException) {
        android.util.Log.e("TAG", "NoCredentialException")
    } catch (e: GetCredentialException) {
        android.util.Log.e("TAG", "GetCredentialException $e")
    } catch (e: Exception) {
        android.util.Log.e("TAG", "Exception ${e.message}")
    }
}

I also tried using ReviewManagerFactory as suggested in this post, but it always tells me that the user is logged in, when he is not:

private fun checkReviewManager(activity: Activity) {
    val reviewManager = ReviewManagerFactory.create(activity)
    val request = reviewManager.requestReviewFlow()
    request.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val reviewInfo = task.result
            val flow = reviewManager.launchReviewFlow(context as Activity, reviewInfo)
            flow.addOnCompleteListener { launchTask ->
                if (launchTask.isSuccessful) {
                    android.util.Log.e("TAG", "User is logged, goto playStore")
                } else {
                    android.util.Log.e("TAG", "User in not logged, goto googleLogin")
                }
            }
        } else {
            android.util.Log.e("TAG", "task Fail: ${task.exception}")
        }
    }
}

Upvotes: 0

Views: 49

Answers (1)

Swati
Swati

Reputation: 1

You're getting NoCeredentialException because Credential Manager doesn't support the functionality that your code intents to find out. Instead try using Google Play In-App Review API. It works only if the user is already logged in, will fail if not.

Hope this helps!

Upvotes: 0

Related Questions