2017-03-13 24 views
0

我按照這裏在AWS的iOS SDK,如何處理FORCE_CHANGE_PASSWORD用戶狀態

https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample

樣品互動cognito登錄集成到我的iOS應用。這一切運行良好,但是當在池中創建新用戶時,它們最初具有FORCE_CHANGE_PASSWORD狀態。

爲Android,你可以按照下面的步驟

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android-sdk-authenticate-admin-created-user.html

但iOS的我不能找出如何做到這一點。使用樣品,如果我嘗試與在FORCE_CHANGE_PASSWORD狀況的用戶登錄,我看到在控制檯日誌下面的輸出(與爲簡潔移除了一些值):

{「ChallengeName」:「NEW_PASSWORD_REQUIRED」,」 ChallengeParameters 「:{」 requiredAttributes 「:」[] 「 」userAttributes「: 」{\「 email_verified \ 」:\「 真正\」,\ 「習俗:autoconfirm \」:\ 「Y \」, 「會話」:」 XYZ「}

以下是從上面詳述的樣品的SignInViewController的代碼。

import Foundation 
import AWSCognitoIdentityProvider 

class SignInViewController: UIViewController { 
    @IBOutlet weak var username: UITextField! 
    @IBOutlet weak var password: UITextField! 
    var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>? 
    var usernameText: String? 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     self.password.text = nil 
     self.username.text = usernameText 
     self.navigationController?.setNavigationBarHidden(true, animated: false) 
    } 

    @IBAction func signInPressed(_ sender: AnyObject) { 
     if (self.username.text != nil && self.password.text != nil) { 
      let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.username.text!, password: self.password.text!) 
      self.passwordAuthenticationCompletion?.set(result: authDetails) 
     } else { 
      let alertController = UIAlertController(title: "Missing information", 
               message: "Please enter a valid user name and password", 
               preferredStyle: .alert) 
      let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil) 
      alertController.addAction(retryAction) 
     } 
    } 
} 

extension SignInViewController: AWSCognitoIdentityPasswordAuthentication { 

    public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>)  { 
     self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource 
     DispatchQueue.main.async { 
      if (self.usernameText == nil) { 
       self.usernameText = authenticationInput.lastKnownUsername 
      } 
     } 
    } 

    public func didCompleteStepWithError(_ error: Error?) { 
     DispatchQueue.main.async { 
     if let error = 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 { 
       self.username.text = nil 
       self.dismiss(animated: true, completion: nil) 
      } 
     } 
    } 
} 

執行didCompleteStepWithError時,error爲空,我希望它指示某些內容告訴我們用戶需要更改密碼。

我的問題是如何捕獲輸出到控制檯的json?"ChallengeName":"NEW_PASSWORD_REQUIRED"

回答

0

Sorted this now。張貼在這裏以防別人幫助其他人。

首先,您需要創建一個視圖控制器,允許用戶指定新的密碼。所述的ViewController應該有一個AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>作爲屬性:

var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>? 

繼在問題中提到的樣品中的圖案,然後我實施AWSCognitoIdentityNewPasswordRequired作爲一個擴展視圖控制器如下:

extension NewPasswordRequiredViewController: AWSCognitoIdentityNewPasswordRequired { 
    func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource: 
    AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>) {      
     self.newPasswordCompletion = newPasswordRequiredCompletionSource 
    } 

    func didCompleteNewPasswordStepWithError(_ error: Error?) { 
     if let error = error as? NSError { 
      // Handle error 
     } else { 
      // Handle success, in my case simply dismiss the view controller 
      self.dismiss(animated: true, completion: nil) 
     } 
    } 

} 

再次按照問題中詳細說明的樣本設計,向012版的AWSCognitoIdentityInteractiveAuthenticationDelegate擴展添加一個函數到AppDelegate:

extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate { 
    func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication { 
     //Existing implementation 
    } 
    func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired { 
     // Your code to handle how the NewPasswordRequiredViewController is created/presented, the view controller should be returned 
     return self.newPasswordRequiredViewController! 
    } 
}