Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Login] Improve error messaging when application password is disabled. #14269

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Yosemite
/// View model for `ApplicationPasswordAuthorizationWebViewController`.
///
final class ApplicationPasswordAuthorizationViewModel {
private let siteURL: String
let siteURL: String
private let stores: StoresManager

init(siteURL: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,20 @@ private extension ApplicationPasswordAuthorizationWebViewController {
guard let url = try await viewModel.fetchAuthURL() else {
DDLogError("⛔️ No authorization URL found for application passwords")
analytics.track(.applicationPasswordAuthorizationURLNotAvailable)
return showErrorAlert(message: Localization.applicationPasswordDisabled)

let errorUI = applicationPasswordDisabledUI(for: viewModel.siteURL)
// When the error view controller is popped, also pop the web view
errorUI.navigationItem.leftBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "chevron.backward"),
style: .plain,
target: self,
action: #selector(popBothControllers)
)

// Push instead of present
navigationController?.pushViewController(errorUI, animated: true)

return
}
loadAuthorizationPage(url: url)
} catch {
Expand All @@ -171,6 +184,12 @@ private extension ApplicationPasswordAuthorizationWebViewController {
}
}

@objc private func popBothControllers() {
// Pop back two view controllers to remove both error and web view
navigationController?.popViewController(animated: false)
navigationController?.popViewController(animated: true)
}

func loadAuthorizationPage(url: URL) {
let parameters: [URLQueryItem] = [
URLQueryItem(name: Constants.Query.appName, value: appName),
Expand Down Expand Up @@ -220,6 +239,14 @@ private extension ApplicationPasswordAuthorizationWebViewController {
}
present(alertController, animated: true)
}

/// The error screen to be displayed when the user tries to log in with site credentials
/// with application password disabled.
///
func applicationPasswordDisabledUI(for siteURL: String) -> UIViewController {
let viewModel = ApplicationPasswordDisabledViewModel(siteURL: siteURL)
return ULErrorViewController(viewModel: viewModel)
}
}

extension ApplicationPasswordAuthorizationWebViewController: WKNavigationDelegate {
Expand Down Expand Up @@ -254,6 +281,13 @@ extension ApplicationPasswordAuthorizationWebViewController: WKNavigationDelegat
}
}

extension ApplicationPasswordAuthorizationWebViewController: UIAdaptivePresentationControllerDelegate {
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
// This will be called when the error UI is dismissed
navigationController?.dismiss(animated: true)
}
}

private extension ApplicationPasswordAuthorizationWebViewController {
enum Constants {
static let successURL = "woocommerce://application-password"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import WordPressAuthenticator
/// modeling an error when application password is disabled.
///
struct ApplicationPasswordDisabledViewModel: ULErrorViewModel {
init(siteURL: String) {
init(siteURL: String,
authentication: Authentication = ServiceLocator.authenticationManager) {
self.siteURL = siteURL
self.authentication = authentication
}

let siteURL: String
let authentication: Authentication
let image: UIImage = .errorImage // TODO: update this if needed

var text: NSAttributedString {
Expand Down Expand Up @@ -55,6 +58,17 @@ struct ApplicationPasswordDisabledViewModel: ULErrorViewModel {
}
WebviewHelper.launch(Constants.applicationPasswordLink, with: viewController)
}

var rightBarButtonItemTitle: String? {
return Localization.helpButtonTitle
}

func didTapRightBarButtonItem(in viewController: UIViewController?) {
guard let viewController else {
return
}
authentication.presentSupport(from: viewController, screen: .noWooError)
}
}

private extension ApplicationPasswordDisabledViewModel {
Expand All @@ -78,6 +92,10 @@ private extension ApplicationPasswordDisabledViewModel {
"Log in with WordPress.com",
comment: "Button that will navigate to the authentication flow with WP.com"
)
static let helpButtonTitle = NSLocalizedString(
"Help",
comment: "Button that will navigate to the support area"
)
}
enum Constants {
static let applicationPasswordLink = "https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/"
Expand Down