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
2 changes: 1 addition & 1 deletion WordPressAuthenticator.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WordPressAuthenticator"
s.version = "1.10.3-beta.1"
s.version = "1.10.3-beta.2"
s.summary = "WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps."

s.description = <<-DESC
Expand Down
4 changes: 2 additions & 2 deletions WordPressAuthenticator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,16 @@
B5609095208A4EAF00399AE4 /* Signin */ = {
isa = PBXGroup;
children = (
98C9195A2308E3D900A90E12 /* AppleAuthenticator.swift */,
B5609126208A563600399AE4 /* EmailMagicLink.storyboard */,
B560912E208A563700399AE4 /* Login.storyboard */,
B5609125208A563600399AE4 /* Login2FAViewController.swift */,
B5609128208A563600399AE4 /* LoginEmailViewController.swift */,
B5609129208A563600399AE4 /* LoginLinkRequestViewController.swift */,
B5609131208A563700399AE4 /* LoginNavigationController.swift */,
982C8E7823021C20003F1BA0 /* LoginPrologueLoginMethodViewController.swift */,
B560912C208A563700399AE4 /* LoginProloguePageViewController.swift */,
B560912B208A563600399AE4 /* LoginProloguePromoViewController.swift */,
982C8E7823021C20003F1BA0 /* LoginPrologueLoginMethodViewController.swift */,
B5609133208A563700399AE4 /* LoginPrologueSignupMethodViewController.swift */,
B5609130208A563700399AE4 /* LoginPrologueViewController.swift */,
B560912A208A563600399AE4 /* LoginSelfHostedViewController.swift */,
Expand All @@ -366,7 +367,6 @@
B5609134208A563700399AE4 /* LoginViewController.swift */,
B5609124208A563600399AE4 /* LoginWPComViewController.swift */,
B560912D208A563700399AE4 /* SigninEditingState.swift */,
98C9195A2308E3D900A90E12 /* AppleAuthenticator.swift */,
);
path = Signin;
sourceTree = "<group>";
Expand Down
132 changes: 65 additions & 67 deletions WordPressAuthenticator/Signin/Login.storyboard

Large diffs are not rendered by default.

162 changes: 26 additions & 136 deletions WordPressAuthenticator/Signin/LoginEmailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {
var didFindSafariSharedCredentials = false
var didRequestSafariSharedCredentials = false
open var offerSignupOption = false
fileprivate var awaitingGoogle = false
private let showNewLoginFlow = WordPressAuthenticator.shared.configuration.showNewLoginFlow

private struct Constants {
Expand Down Expand Up @@ -145,7 +144,7 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {

let button = WPStyleGuide.googleLoginButton()
stackView.addArrangedSubview(button)
button.addTarget(self, action: #selector(googleLoginTapped), for: .touchUpInside)
button.addTarget(self, action: #selector(handleGoogleLoginTapped), for: .touchUpInside)

stackView.addConstraints([
button.leadingAnchor.constraint(equalTo: instructionLabel.leadingAnchor),
Expand All @@ -155,26 +154,6 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {
googleLoginButton = button
}

@objc func googleLoginTapped() {
awaitingGoogle = true
configureViewLoading(true)

GIDSignIn.sharedInstance().disconnect()

// Flag this as a social sign in.
loginFields.meta.socialService = SocialServiceName.google

// Configure all the things and sign in.
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().clientID = WordPressAuthenticator.shared.configuration.googleLoginClientId
GIDSignIn.sharedInstance().serverClientID = WordPressAuthenticator.shared.configuration.googleLoginServerClientId

GIDSignIn.sharedInstance().signIn()

WordPressAuthenticator.track(.loginSocialButtonClick, properties: ["source": "google"])
}

/// Add the log in with site address button to the view
///
func addSelfHostedLogInButton() {
Expand Down Expand Up @@ -321,14 +300,6 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {
}
}


/// Displays the self-hosted sign in form.
///
func loginToSelfHostedSite() {
performSegue(withIdentifier: .showSelfHostedLogin, sender: self)
}


/// Proceeds along the "magic link" sign-in flow, showing a form that let's
/// the user request a magic link.
///
Expand Down Expand Up @@ -404,32 +375,7 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {

override open func displayRemoteError(_ error: Error) {
configureViewLoading(false)

if awaitingGoogle {
awaitingGoogle = false
GIDSignIn.sharedInstance().disconnect()

let errorTitle: String
let errorDescription: String
if (error as NSError).code == WordPressComOAuthError.unknownUser.rawValue {
errorTitle = NSLocalizedString("Connected But…", comment: "Title shown when a user logs in with Google but no matching WordPress.com account is found")
errorDescription = NSLocalizedString("The Google account \"\(loginFields.username)\" doesn't match any account on WordPress.com", comment: "Description shown when a user logs in with Google but no matching WordPress.com account is found")
WordPressAuthenticator.track(.loginSocialErrorUnknownUser)
} else {
errorTitle = NSLocalizedString("Unable To Connect", comment: "Shown when a user logs in with Google but it subsequently fails to work as login to WordPress.com")
errorDescription = error.localizedDescription
}

let socialErrorVC = LoginSocialErrorViewController(title: errorTitle, description: errorDescription)
let socialErrorNav = LoginNavigationController(rootViewController: socialErrorVC)
socialErrorVC.delegate = self
socialErrorVC.loginFields = loginFields
socialErrorVC.modalPresentationStyle = .fullScreen
present(socialErrorNav, animated: true) {}
} else {
errorToPresent = error
performSegue(withIdentifier: .showWPComLogin, sender: self)
}
displayRemoteErrorForGoogle(error)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the error displayed here always a Google error? There was previously a check for if awaitingGoogle... else – was the else case for non-Google errors?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see that the new method includes the else statement!

}


Expand Down Expand Up @@ -484,6 +430,9 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {
}
}

@objc func handleGoogleLoginTapped() {
googleLoginTapped(withDelegate: self)
}

@IBAction func handleSelfHostedButtonTapped(_ sender: UIButton) {
loginToSelfHostedSite()
Expand Down Expand Up @@ -550,101 +499,42 @@ open class LoginEmailViewController: LoginViewController, NUXKeyboardResponder {
}
}

// MARK: - AppleAuthenticatorDelegate

extension LoginEmailViewController: AppleAuthenticatorDelegate {

func showWPComLogin(loginFields: LoginFields) {
self.loginFields = loginFields
performSegue(withIdentifier: .showWPComLogin, sender: self)
}

func authFailedWithError(message: String) {
displayErrorAlert(message, sourceTag: .wpComSignupApple)
}

}

// MARK: - Google Sign In

// LoginFacadeDelegate methods for Google Google Sign In
extension LoginEmailViewController {
func finishedLogin(withGoogleIDToken googleIDToken: String, authToken: String) {
let wpcom = WordPressComCredentials(authToken: authToken, isJetpackLogin: isJetpackLogin, multifactor: false, siteURL: loginFields.siteAddress)
let credentials = AuthenticatorCredentials(wpcom: wpcom)
syncWPComAndPresentEpilogue(credentials: credentials)

// Disconnect now that we're done with Google.
GIDSignIn.sharedInstance().disconnect()
WordPressAuthenticator.track(.signedIn, properties: ["source": "google"])
WordPressAuthenticator.track(.loginSocialSuccess, properties: ["source": "google"])
googleFinishedLogin(withGoogleIDToken: googleIDToken, authToken: authToken)
}


func existingUserNeedsConnection(_ email: String) {
// Disconnect now that we're done with Google.
GIDSignIn.sharedInstance().disconnect()

loginFields.username = email
loginFields.emailAddress = email

performSegue(withIdentifier: .showWPComLogin, sender: self)
WordPressAuthenticator.track(.loginSocialAccountsNeedConnecting, properties: ["source": "google"])
configureViewLoading(false)
googleExistingUserNeedsConnection(email)
}


func needsMultifactorCode(forUserID userID: Int, andNonceInfo nonceInfo: SocialLogin2FANonceInfo) {
loginFields.nonceInfo = nonceInfo
loginFields.nonceUserID = userID

performSegue(withIdentifier: .show2FA, sender: self)
WordPressAuthenticator.track(.loginSocial2faNeeded)
configureViewLoading(false)
googleNeedsMultifactorCode(forUserID: userID, andNonceInfo: nonceInfo)
}
}

extension LoginEmailViewController: GIDSignInDelegate {
open func sign(_ signIn: GIDSignIn?, didSignInFor user: GIDGoogleUser?, withError error: Error?) {
guard let user = user,
let token = user.authentication.idToken,
let email = user.profile.email else {
// The Google SignIn for may have been canceled.
WordPressAuthenticator.track(.loginSocialButtonFailure, error: error)
configureViewLoading(false)
return
}

// Store the email address and token.
loginFields.emailAddress = email
loginFields.username = email
loginFields.meta.socialServiceIDToken = token

loginFacade.loginToWordPressDotCom(withGoogleIDToken: token)
signInGoogleAccount(signIn, didSignInFor: user, withError: error)
}
}

extension LoginEmailViewController: LoginSocialErrorViewControllerDelegate {
private func cleanupAfterSocialErrors() {
dismiss(animated: true) {}
}

func retryWithEmail() {
loginFields.username = ""
cleanupAfterSocialErrors()
}
func retryWithAddress() {
cleanupAfterSocialErrors()
loginToSelfHostedSite()
}
func retryAsSignup() {
cleanupAfterSocialErrors()


let storyboard = UIStoryboard(name: "Signup", bundle: WordPressAuthenticator.bundle)
if let controller = storyboard.instantiateViewController(withIdentifier: "emailEntry") as? SignupEmailViewController {
controller.loginFields = loginFields
navigationController?.pushViewController(controller, animated: true)
}
}
}

/// This is needed to set self as uiDelegate, even though none of the methods are called
extension LoginEmailViewController: GIDSignInUIDelegate {
}

extension LoginEmailViewController: AppleAuthenticatorDelegate {

func showWPComLogin(loginFields: LoginFields) {
self.loginFields = loginFields
performSegue(withIdentifier: .showWPComLogin, sender: self)
}

func authFailedWithError(message: String) {
displayErrorAlert(message, sourceTag: .wpComSignupApple)
}

}
42 changes: 39 additions & 3 deletions WordPressAuthenticator/Signin/LoginPrologueViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import UIKit
import Lottie
import WordPressShared
import WordPressUI
import GoogleSignIn
import WordPressKit

class LoginPrologueViewController: LoginViewController {

Expand Down Expand Up @@ -56,18 +58,18 @@ class LoginPrologueViewController: LoginViewController {
}
vc.modalPresentationStyle = .custom
}

else if let vc = segue.destination as? LoginPrologueLoginMethodViewController {
vc.transitioningDelegate = self

vc.emailTapped = { [weak self] in
self?.performSegue(withIdentifier: .showEmailLogin, sender: self)
}
vc.googleTapped = { [weak self] in
self?.performSegue(withIdentifier: .showGoogle, sender: self)
self?.googleLoginTapped(withDelegate: self)
}
vc.selfHostedTapped = { [weak self] in
self?.performSegue(withIdentifier: .showSelfHostedLogin, sender: self)
self?.loginToSelfHostedSite()
}
vc.appleTapped = { [weak self] in
self?.appleTapped()
Expand Down Expand Up @@ -124,6 +126,8 @@ class LoginPrologueViewController: LoginViewController {

}

// MARK: - AppleAuthenticatorDelegate

extension LoginPrologueViewController: AppleAuthenticatorDelegate {

func showWPComLogin(loginFields: LoginFields) {
Expand All @@ -134,5 +138,37 @@ extension LoginPrologueViewController: AppleAuthenticatorDelegate {
func authFailedWithError(message: String) {
displayErrorAlert(message, sourceTag: .loginApple)
}

}

// MARK: - Google Sign In

// LoginFacadeDelegate methods for Google Google Sign In
extension LoginPrologueViewController {

override open func displayRemoteError(_ error: Error) {
configureViewLoading(false)
displayRemoteErrorForGoogle(error)
}

func finishedLogin(withGoogleIDToken googleIDToken: String, authToken: String) {
googleFinishedLogin(withGoogleIDToken: googleIDToken, authToken: authToken)
}

func existingUserNeedsConnection(_ email: String) {
configureViewLoading(false)
googleExistingUserNeedsConnection(email)
}

func needsMultifactorCode(forUserID userID: Int, andNonceInfo nonceInfo: SocialLogin2FANonceInfo) {
configureViewLoading(false)
googleNeedsMultifactorCode(forUserID: userID, andNonceInfo: nonceInfo)
}

}

extension LoginPrologueViewController: GIDSignInDelegate {
open func sign(_ signIn: GIDSignIn?, didSignInFor user: GIDGoogleUser?, withError error: Error?) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we name this signIn, instead of sign?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method belongs to GIDSignInDelegate (the Google delegate), so... no. 😄 .

Copy link
Contributor

Choose a reason for hiding this comment

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

omg sorry 🙃 :homer-disappear:

signInGoogleAccount(signIn, didSignInFor: user, withError: error)
}
}
Loading