From 43ed8c49008710909588e094f09e1340089383e3 Mon Sep 17 00:00:00 2001 From: lalwani Date: Mon, 23 Dec 2024 09:53:10 -0800 Subject: [PATCH] Handling error code coming in fragment from intent --- .../uber/sdk2/auth/internal/AuthActivity.kt | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt index e93acb3..f3d65d1 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt @@ -85,32 +85,29 @@ class AuthActivity : AppCompatActivity() { startAuth() return } - // otherwise finish the auth flow with error - finishAuthWithError() + // otherwise finish the auth flow with "Canceled" error + finishAuthWithError(CANCELED) } } private fun handleResponse(uri: Uri) { - // This happens when user has authenticated using custom tabs - val authCode = - if (uri.getQueryParameter(KEY_AUTHENTICATION_CODE).isNullOrEmpty() == false) { - uri.getQueryParameter(KEY_AUTHENTICATION_CODE) - } else if (uri.fragment?.isNotEmpty() == true) { - // This happens when user has authenticated using 1p app - Uri.Builder().encodedQuery(uri.fragment).build().getQueryParameter(KEY_AUTHENTICATION_CODE) - } else { - "" - } - if (!authCode.isNullOrEmpty()) { + val authCode = uri.getQueryParameter(KEY_AUTHENTICATION_CODE) + ?: uri.fragment?.takeIf { it.isNotEmpty() } + ?.let { Uri.Builder().encodedQuery(it).build().getQueryParameter(KEY_AUTHENTICATION_CODE) } + ?: "" + + if (authCode.isNotEmpty()) { authProvider?.handleAuthCode(authCode) } else { - // If the intent does not have the auth code, then the user denied the authentication - val error = uri.getQueryParameter(KEY_ERROR) ?: CANCELED + val error = uri.getQueryParameter(KEY_ERROR) + ?: uri.fragment?.takeIf { it.isNotEmpty() } + ?.let { Uri.Builder().encodedQuery(it).build().getQueryParameter(KEY_ERROR) } + ?: CANCELED finishAuthWithError(error) } } - private fun finishAuthWithError(error: String = CANCELED) { + private fun finishAuthWithError(error: String) { // If the intent does not have the auth code, then the user has cancelled the authentication intent.putExtra("EXTRA_ERROR", error) setResult(RESULT_CANCELED, intent)