Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.
Merged
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
8 changes: 7 additions & 1 deletion WordPressAuthenticator/Model/WordPressComSiteInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public class WordPressComSiteInfo {
///
public let url: String

/// Indicates if Jetpack is available, or not,
/// Indicates if Jetpack is available, or not.
///
public let hasJetpack: Bool

/// URL of the Site's Blavatar.
///
public let icon: String

/// Indicates whether the site is WordPressDotCom, or not.
///
public let isWPCom: Bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency could we add a comment describing the property? Its pretty obvious but 🤷‍♂.




Expand All @@ -35,5 +39,7 @@ public class WordPressComSiteInfo {
url = remote["URL"] as? String ?? ""
hasJetpack = remote["jetpack"] as? Bool ?? false
icon = remote["icon.img"] as? String ?? ""
isWPCom = remote["isWordPressDotCom"] as? Bool ?? false

}
}
73 changes: 18 additions & 55 deletions WordPressAuthenticator/Signin/LoginSiteAddressViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,69 +188,32 @@ class LoginSiteAddressViewController: LoginViewController, NUXKeyboardResponder
}
})
}



@objc func fetchSiteInfo() {
let baseSiteUrl = WordPressAuthenticator.baseSiteURL(string: loginFields.siteAddress)
let service = WordPressComBlogService()
let successBlock: (WordPressComSiteInfo) -> Void = { [weak self] siteInfo in
self?.loginFields.meta.siteInfo = siteInfo
if WordPressAuthenticator.shared.delegate?.allowWPComLogin == false {
guard let self = self else {
return
}
self.configureViewLoading(false)
if siteInfo.isWPCom && WordPressAuthenticator.shared.delegate?.allowWPComLogin == false {
// Hey, you have to log out of your existing WP.com account before logging into another one.
self?.promptUserToLogoutBeforeConnectingWPComSite()
self?.configureViewLoading(false)
} else {
self?.configureViewLoading(false)
guard let self = self else {
return
}
self.presentNextControllerIfPossible(siteInfo: siteInfo)
self.promptUserToLogoutBeforeConnectingWPComSite()
return
}
self.presentNextControllerIfPossible(siteInfo: siteInfo)
}

// Is this a WP.com site address?
if let siteAddress = baseSiteUrl.components(separatedBy: "://").last {
let service = WordPressComBlogService()
// Yes. Then let's attempt to grab the site info.
service.fetchSiteInfo(for: siteAddress, success: successBlock, failure: { [weak self] (error) in
// If fetchSiteInfo failed because the site is private (errorCode == .authorizationRequired),
// then we try to fetch the site info with a call to the `connect/site-info` endpoint.
// If this call succeeds, we check if login is allowed.
let originalError = error as NSError
let errorCode = WordPressComRestApiError(rawValue: originalError.code)
if errorCode == .authorizationRequired {
service.fetchUnauthenticatedSiteInfoForAddress(for: baseSiteUrl, success: successBlock, failure: { error in
// The un-authed site info request failed.
self?.configureViewLoading(false)
guard let self = self else {
return
}

self.presentNextControllerIfPossible(siteInfo: nil)
})
} else {
// Failed to get the site info.
self?.configureViewLoading(false)
guard let self = self else {
return
}

self.presentNextControllerIfPossible(siteInfo: nil)
}
})
} else {
// Not a WP.com site. Let's make an un-authenticated site info request.
let service = WordPressComBlogService()
service.fetchUnauthenticatedSiteInfoForAddress(for: baseSiteUrl, success: successBlock, failure: { [weak self] error in
self?.configureViewLoading(false)
guard let self = self else {
return
}

self.presentNextControllerIfPossible(siteInfo: nil)
})
}
service.fetchUnauthenticatedSiteInfoForAddress(for: baseSiteUrl, success: successBlock, failure: { [weak self] error in
self?.configureViewLoading(false)
guard let self = self else {
return
}
self.presentNextControllerIfPossible(siteInfo: nil)
})
}


func presentNextControllerIfPossible(siteInfo: WordPressComSiteInfo?) {
WordPressAuthenticator.shared.delegate?.shouldPresentUsernamePasswordController(for: siteInfo, onCompletion: { (error, isSelfHosted) in
guard let originalError = error else {
Expand Down