Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit c3262a9

Browse files
authored
Site credential login entry point and site info fetching (#680)
2 parents 34a4bf5 + 6071628 commit c3262a9

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

WordPressAuthenticator.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = 'WordPressAuthenticator'
5-
s.version = '3.1.0'
5+
s.version = '3.2.0-beta.1'
66

77
s.summary = 'WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps.'
88
s.description = <<-DESC

WordPressAuthenticator/Authenticator/WordPressAuthenticator.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,48 @@ import WordPressKit
223223
presenter.present(navController, animated: true, completion: nil)
224224
}
225225

226+
/// Used to present the site credential login flow directly from the delegate.
227+
///
228+
/// - Parameters:
229+
/// - presenter: The view controller that presents the site credential login flow.
230+
/// - siteURL: The URL of the site to log in to.
231+
/// - onCompletion: The closure to be trigged when the login succeeds with the input credentials.
232+
///
233+
public class func showSiteCredentialLogin(from presenter: UIViewController, siteURL: String, onCompletion: @escaping (WordPressOrgCredentials) -> Void) {
234+
let controller = SiteCredentialsViewController.instantiate(from: .siteAddress) { coder in
235+
SiteCredentialsViewController(coder: coder, isDismissible: true, onCompletion: onCompletion)
236+
}
237+
guard let controller = controller else {
238+
DDLogError("Failed to navigate from GetStartedViewController to SiteCredentialsViewController")
239+
return
240+
}
241+
242+
let loginFields = LoginFields()
243+
loginFields.siteAddress = siteURL
244+
controller.loginFields = loginFields
245+
controller.dismissBlock = { _ in
246+
controller.navigationController?.dismiss(animated: true)
247+
}
248+
249+
let navController = LoginNavigationController(rootViewController: controller)
250+
navController.modalPresentationStyle = .fullScreen
251+
presenter.present(navController, animated: true, completion: nil)
252+
}
253+
254+
/// A helper method to fetch site info for a given URL.
255+
/// - Parameters:
256+
/// - siteURL: The URL of the site to fetch information for.
257+
/// - onCompletion: The closure to be triggered when fetching site info is done.
258+
///
259+
public class func fetchSiteInfo(for siteURL: String, onCompletion: @escaping (Result<WordPressComSiteInfo, Error>) -> Void) {
260+
let service = WordPressComBlogService()
261+
service.fetchUnauthenticatedSiteInfoForAddress(for: siteURL, success: { siteInfo in
262+
onCompletion(.success(siteInfo))
263+
}, failure: { error in
264+
onCompletion(.failure(error))
265+
})
266+
}
267+
226268
/// Shows the unified Login/Signup flow.
227269
///
228270
private class func showGetStarted(from presenter: UIViewController, jetpackLogin: Bool, connectedEmail: String? = nil, siteURL: String? = nil) {

WordPressAuthenticator/Unified Auth/View Related/Site Address/SiteCredentialsViewController.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ final class SiteCredentialsViewController: LoginViewController {
1616
private var errorMessage: String?
1717
private var shouldChangeVoiceOverFocus: Bool = false
1818

19+
private let isDismissible: Bool
20+
private let completionHandler: ((WordPressOrgCredentials) -> Void)?
21+
22+
init?(coder: NSCoder, isDismissible: Bool, onCompletion: @escaping (WordPressOrgCredentials) -> Void) {
23+
self.isDismissible = isDismissible
24+
self.completionHandler = onCompletion
25+
super.init(coder: coder)
26+
}
27+
28+
required init?(coder: NSCoder) {
29+
self.isDismissible = false
30+
self.completionHandler = nil
31+
super.init(coder: coder)
32+
}
33+
1934
// Required for `NUXKeyboardResponder` but unused here.
2035
var verticalCenterConstraint: NSLayoutConstraint?
2136

@@ -46,6 +61,9 @@ final class SiteCredentialsViewController: LoginViewController {
4661
loginFields.meta.userIsDotCom = false
4762

4863
navigationItem.title = WordPressAuthenticator.shared.displayStrings.logInTitle
64+
if isDismissible {
65+
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismissView))
66+
}
4967
styleNavigationBar(forUnified: true)
5068

5169
// Store default margin, and size table for the view.
@@ -221,6 +239,9 @@ extension SiteCredentialsViewController: UITextFieldDelegate {
221239
// MARK: - Private Methods
222240
private extension SiteCredentialsViewController {
223241

242+
@objc func dismissView() {
243+
dismissBlock?(true)
244+
}
224245
/// Registers all of the available TableViewCells.
225246
///
226247
func registerTableViewCells() {
@@ -478,6 +499,9 @@ extension SiteCredentialsViewController {
478499
// Try to get the jetpack email from XML-RPC response dictionary.
479500
//
480501
guard let loginFields = makeLoginFieldsUsing(xmlrpc: xmlrpc, options: options) else {
502+
if let completionHandler = completionHandler {
503+
return completionHandler(wporg)
504+
}
481505
DDLogError("Unexpected response from .org site credentials sign in using XMLRPC.")
482506
showLoginEpilogue(for: credentials)
483507
return

0 commit comments

Comments
 (0)