googlesamples / google-services

A collection of quickstart samples demonstrating the Google APIs for Android and iOS
https://developers.google.com
Apache License 2.0
3.07k stars 2.53k forks source link

Endless loop of signin #576

Open alexplanetart opened 1 year ago

alexplanetart commented 1 year ago

Endless loop when integrating Google Sign-In with two scopes (https://www.googleapis.com/auth/photoslibrary.readonly https://www.googleapis.com/auth/drive.readonly) into My Android App

https://github.com/googlesamples/google-services/assets/93108740/9c26897f-1c9c-4466-b056-cc4d7c645c1c

Steps to reproduce:

  1. Launch Google SignIn with two scopes
  2. Only select "View your Google photos library" option and remain "See and download all your Google Drive files" option unselected

Observed Results:

Expected Results:

Relevant Code:

fun initGoogleLogin() {
    val config = GalleryLib.galleryConfig.googleConfig
        ?: throw IllegalArgumentException("Add googleConfig param in GalleryLib.init()")

    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(
                    Scope("https://www.googleapis.com/auth/photoslibrary.readonly"),
                    Scope("https://www.googleapis.com/auth/drive.readonly")
            )
            .requestEmail()
            .requestServerAuthCode(config.googleId, true)//request refres_token everytime!!!
            .build()

    val client = GoogleSignIn.getClient(requireActivity(), gso)

    googleSignInLauncher.launch(client.signInIntent)

}

private val googleSignInLauncher =
    registerForActivityResult(GoogleSignInResultContract()) { account ->
        val code = account?.serverAuthCode
        if (code.isNullOrEmpty().not()) {
            showLoading(true)
            launch {
                val account = viewmodel.requestToken(code!!)
                showLoading(false)

                if (account != null) {
                    onSignInSuccess()
                } else {
                    onSignInFailure(RuntimeException("Google sign in failed"))
                }
            }
        } else {
            onSignInFailure(RuntimeException("Google sign in canceled"))
        }
    }

class GoogleSignInResultContract : ActivityResultContract<Intent, GoogleSignInAccount?>() {
    override fun createIntent(context: Context, input: Intent): Intent {
        return input
    }

    override fun parseResult(resultCode: Int, intent: Intent?): GoogleSignInAccount? {
        return if (resultCode == Activity.RESULT_OK) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(intent)

            val account = try {
                task.getResult(ApiException::class.java)
            } catch (e: ApiException) {
                null
            }

            account
        } else {
            null
        }

    }
}
alexplanetart commented 1 year ago

Any replies? What's even more strange is that when I only request for photo permission and permission page reloads even if the photo permission is granted.

https://github.com/googlesamples/google-services/assets/93108740/2dedf4c8-2af6-4806-b1f2-3d3bbdf1f837

alexplanetart commented 1 year ago

Endless loop only happens with Samsung Galaxy S23 Pro and signin succeed with other Android phones.