【问题标题】:AWS Cognito Swift SDK How can I check if a user is confirmed?AWS Cognito Swift SDK 如何检查用户是否已确认?
【发布时间】:2018-08-06 16:22:33
【问题描述】:

我对 Cognito 很陌生。我刚刚使用提供的示例成功设置了注册系统,并且可以使用我的 iOS 应用程序注册用户。

我要求用户在注册期间验证电子邮件。

他们的数据显示在 Cognito 控制台上,并且在选择验证电子邮件中的链接之前,他们的状态是未确认的。

在我的应用中,我不希望用户在验证电子邮件之前能够使用该应用。

如何查看用户何时点击了电子邮件确认?我不想在用户注册后将用户路由回登录页面。我将他们带到一个页面,他们可以在确认电子邮件后按继续。

我尝试了以下方法:

self.user!.getSession(self.username, password: self.password, validationData: nil).continueWith(block: { (session) -> Any? in
    if (self.user!.confirmedStatus == AWSCognitoIdentityUserStatus.confirmed) {
        print("confirmed")
    } else if (self.user!.confirmedStatus == AWSCognitoIdentityUserStatus.unconfirmed) {
        print("unconfirmed")
    } else if (self.user!.confirmedStatus == AWSCognitoIdentityUserStatus.unknown) {
        print("unknown")
    } else {
        print("elsecase")
    }
    return nil
})

出于某种原因,即使用户未确认,也已确认打印。

对这一切都很陌生,我真的不确定在我的场景中采用什么方法来查看用户何时点击了他们的验证链接。

【问题讨论】:

    标签: ios swift xcode authentication amazon-cognito


    【解决方案1】:

    使用 userAttributes 数组获取所有用户属性,包括“email_verified”标志

    在 CognitoYourUserPoolSample 项目中,UserDetailsTableViewController 类有一个名为 refresh() 的方法,该方法用于在登录后获取用户详细信息。我添加了几行代码来识别用户是否确认电子邮件地址。

      func refresh() {
        self.user?.getDetails().continueOnSuccessWith { (task) -> AnyObject? in
            DispatchQueue.main.async(execute: {
                self.response = task.result
                self.title = self.user?.username
                self.tableView.reloadData()
    
                // Get user attributes and check the "email_verified" flag
                let attributes = self.response?.userAttributes
                if let emailVerifiedAttribute = attributes?.filter({$0.name == "email_verified"}), emailVerifiedAttribute.count == 1  {
                    if (emailVerifiedAttribute[0].value == "false") {
                        print("Email not verified")
                        self.openEmailVerificationVc()
                    }else{
                        print("Email  verified")
                    }
    
                }
            })
    
    
            return nil
        }
    }
    func openEmailVerificationVc(){
    
        user?.getAttributeVerificationCode("email")
        // Show email verification vc here,
    
    }
    

    从电子邮件验证视图控制器调用“verifyAttribute”方法

    var user: AWSCognitoIdentityUser?
    var pool: AWSCognitoIdentityUserPool?  
    
    override func viewDidLoad() {
            super.viewDidLoad()
    
            self.pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)
            if (self.user == nil) {
                self.user = self.pool?.currentUser()
            }
    
    
    
        }
    func verifyEmailWithCode(code:String){
        user?.verifyAttribute("email", code: code)
    }
    

    【讨论】:

    • 调用 getDetails 时出现错误:错误域 = com.amazonaws.AWSCognitoIdentityProviderErrorDomain Code=-1000 “必须在 AWSCognitoIdentityInteractiveAuthenticationDelegate 上实施 startCustomAuthentication 或 startPasswordAuthentication” UserInfo={NSLocalizedDescription=startCustomAuthentication 或 startPasswordAuthentication必须在您的 AWSCognitoIdentityInteractiveAuthenticationDelegate} 上实施
    • 这可能是 AWS 控制台中的 UserPool 设置的问题。在 AWS 控制台-用户池设置中选择所需的复选框
    • 嘿,酷。谢谢。关于需要哪些复选框的任何建议?由于我无法访问该项目的 AWS 控制台(我们的客户可以),因此我操作有点盲目。
    【解决方案2】:

    您只需在回调中检查此值:

    task.result?.userConfirmed?.boolValue
    

    无论用户是否被确认,这将返回真或假。希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      如上所述,getDetailsgetSession 都没有成功检查用户是否已确认。我正在做的是打电话给AWSMobileClient.sharedInstance().signIn

      如果我收到错误:AWSMobileClientError.userNotConfirmed,那么我认为这表明用户尚未确认。

      如果我成功让用户登录,那么我认为这表明用户已经确认。

      当然,这有让用户登录的副作用。我最初试图在此之后将用户注销,作为signIn 回调的一部分,但这有@987654326 的奇怪副作用@回调调用了两次!

      【讨论】:

        猜你喜欢
        • 2017-04-08
        • 2021-08-28
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 1970-01-01
        • 2021-02-19
        • 1970-01-01
        相关资源
        最近更新 更多