Search code examples
swiftaws-userpools

swift: AWSCognitoIdentityInteractiveAuthenticationDelegate methods are not called in the login view controller


I am trying to login using AWSCognito User pool but the AWSCognitoIdentityInteractiveAuthenticationDelegate methods are not being called. Here is my code, where am I doing wrong?

import UIKit
import AWSCognito
import AWSCognitoIdentityProvider

class LoginViewController: UIViewController,     AWSCognitoIdentityInteractiveAuthenticationDelegate {

@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
var user: AWSCognitoIdentityUser?

override func viewDidLoad() {
    super.viewDidLoad()

    loginButton.layer.cornerRadius = 10
    loginButton.layer.borderWidth = 1

    pool.delegate = self
}

@IBAction func loginTap(_ sender: Any) {
    if let email = emailTextField.text, let password = passwordTextField.text {
        if email.isValidEmail() {
            let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails.init(username: email, password: password)
            self.passwordAuthenticationCompletion?.set(result: authDetails)
            self.user?.getSession()
        } else {
            let alert = UIAlertController(title: "Alert", message: "Invalid Email or password", preferredStyle: UIAlertController.Style.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

@IBAction func cancelButtonTap(_ sender: Any) {
    dismiss(animated: true, completion: nil)
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        NotificationCenter.default.post(name: NSNotification.Name("goToPaymentPageVC"), object: nil)
    }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    emailTextField.resignFirstResponder()
    passwordTextField.resignFirstResponder()
}


//:- MARK: AWSCognitoIdentityInteractiveAuthenticationDelegate methods
func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
    self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
    DispatchQueue.main.async {
        if (self.emailTextField.text == nil) {
            self.emailTextField.text = authenticationInput.lastKnownUsername
        }
    }
}

func didCompleteStepWithError(_ error: Error?) {
    DispatchQueue.main.async {
        if let error = error as NSError? {
            let alertController = UIAlertController(title: "Error",
                                                    message: error.userInfo["message"] as? String,
                                                    preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)

            self.present(alertController, animated: true, completion:  nil)
        } else {
            self.emailTextField.text = nil
            self.dismiss(animated: true, completion: nil)
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                NotificationCenter.default.post(name: NSNotification.Name("goToPaymentPageVC"), object: nil)
            }
        }
    }
}
}

Expected result: AWSCognitoIdentityInteractiveAuthenticationDelegate methods should be called and the loginViewController should dismiss.


Solution

  • This is how it was fixed:

    1. Conform the class LoginViewController to AWSCognitoIdentityPasswordAuthentication and added the following function in the same class

      func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication { return self }

    2. Adding pool.clearAll()before calling self.user?.getSession()