Add an AccountProvider type and use it within the authentication flow. (#6103)

* Add an AccountProvider type and use it within AppSettings.

* Use the AccountProvider type within the Authentication flow too.

* Fix unit test compilation with Swift 6.4

* Make sure defaultAccountProvider takes allowOtherAccountProviders into account.

* Document a caveat on using AuthenticationService.homeserver.value.accountProvider.
This commit is contained in:
Doug
2026-09-14 15:25:33 +01:00
committed by GitHub
parent 26fddb8aa2
commit 64aae09c6d
29 changed files with 442 additions and 265 deletions
@@ -41,13 +41,12 @@ class ServerSelectionScreenViewModel: ServerSelectionScreenViewModelType, Server
self.userIndicatorController = userIndicatorController
self.homeserverHistoryManager = homeserverHistoryManager
let homeserverAddress: String
if case .picker(let providers) = mode {
homeserverAddress = providers[0]
let serverNameOrBaseURL = if case .picker(let providers) = mode {
providers[0]
} else {
homeserverAddress = authenticationService.homeserver.value.address
authenticationService.homeserver.value.accountProvider.serverNameOrBaseURL
}
let bindings = ServerSelectionScreenBindings(homeserverAddress: homeserverAddress)
let bindings = ServerSelectionScreenBindings(serverNameOrBaseURL: serverNameOrBaseURL)
super.init(initialViewState: ServerSelectionScreenViewState(mode: mode, authenticationFlow: authenticationFlow, bindings: bindings))
context.viewState.textFieldAdapter.keystrokePublisher
@@ -80,40 +79,40 @@ class ServerSelectionScreenViewModel: ServerSelectionScreenViewModelType, Server
// MARK: - Private
private func pickServer() async {
let accountProvider = state.bindings.homeserverAddress
guard accountProvider.isEmpty == false else {
let serverNameOrBaseURL = state.bindings.serverNameOrBaseURL
guard serverNameOrBaseURL.isEmpty == false else {
fatalError("It shouldn't be possible to confirm without a selection.")
}
startLoading()
switch await authenticationService.configure(for: accountProvider, flow: authenticationFlow) {
switch await authenticationService.configure(for: serverNameOrBaseURL, flow: authenticationFlow) {
case .success:
MXLog.info("Selected server: \(accountProvider)")
MXLog.info("Selected server: \(serverNameOrBaseURL)")
await fetchLoginURLIfNeededAndContinue()
stopLoading()
case .failure:
MXLog.info("Invalid server: \(accountProvider)")
MXLog.info("Invalid server: \(serverNameOrBaseURL)")
stopLoading()
// When the servers are hard-coded they should have a valid configuration, so show a generic error.
state.bindings.alertInfo = AlertInfo(id: .unknownError)
}
}
/// Updates the login flow using the supplied homeserver address, or shows an error when this isn't possible.
/// Updates the login flow using the supplied server name or base URL, or shows an error when this isn't possible.
private func configureHomeserver() async {
let userInput = state.bindings.homeserverAddress
let userInput = state.bindings.serverNameOrBaseURL
// People often enter their Matrix ID here, so use the server name from it when they do.
let homeserverAddress = (try? serverNameFromUserId(userId: userInput)) ?? userInput
let serverNameOrBaseURL = (try? serverNameFromUserId(userId: userInput)) ?? userInput
startLoading()
switch await authenticationService.configure(for: homeserverAddress, flow: authenticationFlow) {
switch await authenticationService.configure(for: serverNameOrBaseURL, flow: authenticationFlow) {
case .success:
MXLog.info("Selected homeserver: \(homeserverAddress)")
MXLog.info("Selected homeserver: \(serverNameOrBaseURL)")
await fetchLoginURLIfNeededAndContinue()
stopLoading()
case .failure(let error):
MXLog.info("Invalid homeserver: \(homeserverAddress)")
MXLog.info("Invalid homeserver: \(serverNameOrBaseURL)")
stopLoading()
handleError(error)
}
@@ -153,7 +152,7 @@ class ServerSelectionScreenViewModel: ServerSelectionScreenViewModelType, Server
/// Processes an error to either update the flow or display it to the user.
private func handleError(_ error: AuthenticationServiceError) {
switch error {
case .invalidServer, .invalidHomeserverAddress:
case .invalidServer, .invalidServerNameOrBaseURL:
showFooterMessage(L10n.screenChangeServerErrorInvalidHomeserver)
case .invalidWellKnown(let error):
state.bindings.alertInfo = AlertInfo(id: .invalidWellKnownAlert(error),
@@ -221,9 +220,9 @@ class ServerSelectionScreenViewModel: ServerSelectionScreenViewModelType, Server
debouncedAutocompleteTask = Task { @MainActor in
try? await Task.sleep(for: .milliseconds(10))
guard Task.isCancelled == false else { return }
context.homeserverAddress = newInput
context.serverNameOrBaseURL = newInput
context.viewState.textField?.text = newInput
context.homeserverSelection = TextSelection(range: selectionRange)
context.serverNameOrBaseURLSelection = TextSelection(range: selectionRange)
}
}
}