使用AWS Cognito,Swift进行自定义身份验证

时间:2017-11-19 03:08:10

标签: swift amazon-web-services aws-cognito

我正在尝试使用AWS Cognito进行密码登录/使用手机号码注册。

登录/注册步骤:

  1. 用户仅提交手机号码

  2. 通过短信收到密码后,用户提交登录/注册信息。

  3. 要实现上述目标,由于没有仅针对手机号码Signin的示例代码,我正在尝试修改此aws sdk github example中显示的标准注册(电子邮件,密码)代码。

    有人可以建议您只通过手机号码(无电子邮件或用户名)进行更改。

    import Foundation
    import AWSCognitoIdentityProvider
    
    class SignUpViewController: UIViewController {
    
        var pool: AWSCognitoIdentityUserPool?
        var sentTo: String?
    
        @IBOutlet weak var username: UITextField!
        @IBOutlet weak var password: UITextField!
    
        @IBOutlet weak var phone: UITextField!
        @IBOutlet weak var email: UITextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.pool = AWSCognitoIdentityUserPool.init(forKey: AWSCognitoUserPoolsSignInProviderKey)
        }
    
        override func viewWillAppear(_ animated: Bool) {
            self.navigationController?.setNavigationBarHidden(false, animated: false)
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let signUpConfirmationViewController = segue.destination as? ConfirmSignUpViewController {
                signUpConfirmationViewController.sentTo = self.sentTo
                signUpConfirmationViewController.user = self.pool?.getUser(self.username.text!)
            }
        }
    
        @IBAction func signUp(_ sender: AnyObject) {
    
            guard let userNameValue = self.username.text, !userNameValue.isEmpty,
                let passwordValue = self.password.text, !passwordValue.isEmpty else {
                    let alertController = UIAlertController(title: "Missing Required Fields",
                                                            message: "Username / Password are required for registration.",
                                                            preferredStyle: .alert)
                    let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
                    alertController.addAction(okAction)
    
                    self.present(alertController, animated: true, completion:  nil)
                    return
            }
    
            var attributes = [AWSCognitoIdentityUserAttributeType]()
    
            if let phoneValue = self.phone.text, !phoneValue.isEmpty {
                let phone = AWSCognitoIdentityUserAttributeType()
                phone?.name = "phone_number"
                phone?.value = phoneValue
                attributes.append(phone!)
            }
    
            if let emailValue = self.email.text, !emailValue.isEmpty {
                let email = AWSCognitoIdentityUserAttributeType()
                email?.name = "email"
                email?.value = emailValue
                attributes.append(email!)
            }
    
    
    
            //sign up the user
            self.pool?.signUp(userNameValue, password: passwordValue, userAttributes: attributes, validationData: nil).continueWith {[weak self] (task) -> Any? in
                guard let strongSelf = self else { return nil }
                DispatchQueue.main.async(execute: {
                    if let error = task.error as? NSError {
                        let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
                                                                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 if let result = task.result  {
                        // handle the case where user has to confirm his identity via email / SMS
                        if (result.user.confirmedStatus != AWSCognitoIdentityUserStatus.confirmed) {
                            strongSelf.sentTo = result.codeDeliveryDetails?.destination
                            strongSelf.performSegue(withIdentifier: "confirmSignUpSegue", sender:sender)
                        } else {
                            let _ = strongSelf.navigationController?.popToRootViewController(animated: true)
                        }
                    }
    
                })
                return nil
            }
        }
    }
    

0 个答案:

没有答案