Skip to content

Commit 43ed8c4

Browse files
committed
Handling error code coming in fragment from intent
1 parent e664878 commit 43ed8c4

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,29 @@ class AuthActivity : AppCompatActivity() {
8585
startAuth()
8686
return
8787
}
88-
// otherwise finish the auth flow with error
89-
finishAuthWithError()
88+
// otherwise finish the auth flow with "Canceled" error
89+
finishAuthWithError(CANCELED)
9090
}
9191
}
9292

9393
private fun handleResponse(uri: Uri) {
94-
// This happens when user has authenticated using custom tabs
95-
val authCode =
96-
if (uri.getQueryParameter(KEY_AUTHENTICATION_CODE).isNullOrEmpty() == false) {
97-
uri.getQueryParameter(KEY_AUTHENTICATION_CODE)
98-
} else if (uri.fragment?.isNotEmpty() == true) {
99-
// This happens when user has authenticated using 1p app
100-
Uri.Builder().encodedQuery(uri.fragment).build().getQueryParameter(KEY_AUTHENTICATION_CODE)
101-
} else {
102-
""
103-
}
104-
if (!authCode.isNullOrEmpty()) {
94+
val authCode = uri.getQueryParameter(KEY_AUTHENTICATION_CODE)
95+
?: uri.fragment?.takeIf { it.isNotEmpty() }
96+
?.let { Uri.Builder().encodedQuery(it).build().getQueryParameter(KEY_AUTHENTICATION_CODE) }
97+
?: ""
98+
99+
if (authCode.isNotEmpty()) {
105100
authProvider?.handleAuthCode(authCode)
106101
} else {
107-
// If the intent does not have the auth code, then the user denied the authentication
108-
val error = uri.getQueryParameter(KEY_ERROR) ?: CANCELED
102+
val error = uri.getQueryParameter(KEY_ERROR)
103+
?: uri.fragment?.takeIf { it.isNotEmpty() }
104+
?.let { Uri.Builder().encodedQuery(it).build().getQueryParameter(KEY_ERROR) }
105+
?: CANCELED
109106
finishAuthWithError(error)
110107
}
111108
}
112109

113-
private fun finishAuthWithError(error: String = CANCELED) {
110+
private fun finishAuthWithError(error: String) {
114111
// If the intent does not have the auth code, then the user has cancelled the authentication
115112
intent.putExtra("EXTRA_ERROR", error)
116113
setResult(RESULT_CANCELED, intent)

0 commit comments

Comments
 (0)