Skip to content

Commit e99ac7d

Browse files
@W-20161958: [MSDK 13.1][Android] Cannot login GUS using Welcome endpoint
1 parent 924877c commit e99ac7d

1 file changed

Lines changed: 56 additions & 43 deletions

File tree

libs/SalesforceSDK/src/com/salesforce/androidsdk/ui/LoginActivity.kt

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ import java.security.cert.X509Certificate
173173
* them.
174174
*/
175175
open class LoginActivity : FragmentActivity() {
176+
private var customTabLauncher: ActivityResultLauncher<Intent>? = null
177+
176178
// View Model
177179
@VisibleForTesting(otherwise = PROTECTED)
178180
open val viewModel: LoginViewModel
@@ -270,7 +272,7 @@ open class LoginActivity : FragmentActivity() {
270272
onBackPressedDispatcher.addCallback { handleBackBehavior() }
271273
}
272274

273-
val customTabLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
275+
customTabLauncher = registerForActivityResult(
274276
ActivityResultContracts.StartActivityForResult()
275277
) { result: ActivityResult ->
276278
// Check if the user backed out of the custom tab.
@@ -285,48 +287,6 @@ open class LoginActivity : FragmentActivity() {
285287
}
286288
}
287289

288-
// Take action on selected server change.
289-
viewModel.selectedServer.observe(this) { selectedServer ->
290-
291-
// Guard against observing a selected server already provided by the intent data, such as a Salesforce Welcome Discovery mobile URL.
292-
val selectedServerUri = selectedServer.toUri()
293-
if (intent.data?.host == selectedServerUri.host) {
294-
return@observe
295-
}
296-
297-
// Use the URL to switch between default or Salesforce Welcome Discovery log in, if applicable.
298-
if (switchDefaultOrSalesforceWelcomeDiscoveryLogin(selectedServerUri)) {
299-
return@observe
300-
}
301-
302-
if (viewModel.singleServerCustomTabActivity) {
303-
// Skip fetching authorization and show custom tab immediately.
304-
viewModel.reloadWebView()
305-
viewModel.loginUrl.value?.let { url ->
306-
loadLoginPageInCustomTab(url, customTabLauncher)
307-
}
308-
} else {
309-
with(SalesforceSDKManager.getInstance()) {
310-
// Fetch well known config and load in custom tab if required.
311-
fetchAuthenticationConfiguration {
312-
/* Browser-based authentication is applicable when not authenticating with a front-door bridge URL */
313-
if (isBrowserLoginEnabled && !viewModel.isUsingFrontDoorBridge) {
314-
if (useWebServerAuthentication) {
315-
viewModel.loginUrl.value?.let { url -> loadLoginPageInCustomTab(url, customTabLauncher) }
316-
} else {
317-
/* Reload the webview now that isBrowserLoginEnabled has been set
318-
to true so that we generate an authorization URL with PKCE values. */
319-
lifecycleScope.launch(Dispatchers.Main) {
320-
viewModel.reloadWebView()
321-
viewModel.loginUrl.value?.let { url -> loadLoginPageInCustomTab(url, customTabLauncher) }
322-
}
323-
}
324-
}
325-
}
326-
}
327-
}
328-
}
329-
330290
// Support magic links
331291
if (viewModel.jwt != null) {
332292
swapJWTForAccessToken()
@@ -1097,6 +1057,59 @@ open class LoginActivity : FragmentActivity() {
10971057

10981058
// If the intent is for log in using a UI Bridge API front door URL, apply it to the activity.
10991059
applyUiBridgeApiFrontDoorUrl(intent)
1060+
1061+
applySelectedServer()
1062+
}
1063+
1064+
/**
1065+
* Applies the view model's selected server to the activity.
1066+
*/
1067+
private fun applySelectedServer() {
1068+
1069+
// Reset the selected login server observer to consume the latest and future values.
1070+
viewModel.selectedServer.removeObservers(this)
1071+
viewModel.selectedServer.observe(this) { selectedServer ->
1072+
1073+
// Guard against observing a selected server already provided by the intent data, such as a Salesforce Welcome Discovery mobile URL.
1074+
val selectedServerUri = selectedServer.toUri()
1075+
if (intent.data?.host == selectedServerUri.host) {
1076+
return@observe
1077+
}
1078+
1079+
// Use the URL to switch between default or Salesforce Welcome Discovery log in, if applicable.
1080+
if (switchDefaultOrSalesforceWelcomeDiscoveryLogin(selectedServerUri)) {
1081+
return@observe
1082+
}
1083+
1084+
if (viewModel.singleServerCustomTabActivity) {
1085+
// Skip fetching authorization and show custom tab immediately.
1086+
viewModel.reloadWebView()
1087+
viewModel.loginUrl.value?.let { url ->
1088+
customTabLauncher?.let { loadLoginPageInCustomTab(url, it) }
1089+
}
1090+
} else {
1091+
with(SalesforceSDKManager.getInstance()) {
1092+
// Fetch well known config and load in custom tab if required.
1093+
fetchAuthenticationConfiguration {
1094+
/* Browser-based authentication is applicable when not authenticating with a front-door bridge URL */
1095+
if (isBrowserLoginEnabled && !viewModel.isUsingFrontDoorBridge) {
1096+
if (useWebServerAuthentication) {
1097+
viewModel.loginUrl.value?.let { url ->
1098+
customTabLauncher?.let { loadLoginPageInCustomTab(url, it) }
1099+
}
1100+
} else {
1101+
/* Reload the webview now that isBrowserLoginEnabled has been set
1102+
to true so that we generate an authorization URL with PKCE values. */
1103+
lifecycleScope.launch(Dispatchers.Main) {
1104+
viewModel.reloadWebView()
1105+
viewModel.loginUrl.value?.let { url -> customTabLauncher?.let { loadLoginPageInCustomTab(url, it) } }
1106+
}
1107+
}
1108+
}
1109+
}
1110+
}
1111+
}
1112+
}
11001113
}
11011114

11021115
/**

0 commit comments

Comments
 (0)