简体   繁体   English

AWS Cognito Swift SDK如何检查用户是否已确认?

[英]AWS Cognito Swift SDK How can I check if a user is confirmed?

I am very new to Cognito. 我是Cognito的新手。 I have just successfully setup a signUp system using the examples provided and can use my iOS app to signUp users. 我刚刚使用提供的示例成功设置了signUp系统,可以使用我的iOS应用程序为用户注册。

I require the users to verify an email during the signup. 我要求用户在注册期间验证电子邮件。

Their data appears on the Cognito console and prior to selecting the link in the verification email, their status is unconfirmed. 他们的数据显示在Cognito控制台上,在选择验证邮件中的链接之前,他们的状态未经证实。

In my app I don't want the user to be able to use the app until they verify their email. 在我的应用程序中,我不希望用户在验证他们的电子邮件之前能够使用该应用程序。

How can I check when the user has clicked on the email confirmation? 如何检查用户何时点击了电子邮件确认? I don't want to route the user back to the login page after they sign up. 我不想在用户注册后将用户路由回登录页面。 I take them to a page where they can press continue after confirming their email. 我将他们带到一个页面,他们可以在确认他们的电子邮件后继续按下。

I have tried the following: 我尝试过以下方法:

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
})

For some reason this is printing confirmed even though the user is unconfirmed. 出于某种原因,即使用户未经证实,也会确认打印。

Being very new to all this I'm really not sure what approach to take in my scenario to see when the user has clicked on their verification link. 作为这一切的新手,我真的不确定在我的场景中采用什么方法来查看用户何时点击了他们的验证链接。

Use userAttributes array to get all user attributes including " email_verified " flag 使用userAttributes数组获取所有用户属性,包括“ email_verified ”标志

In CognitoYourUserPoolSample Project the UserDetailsTableViewController class have a method named refresh(), this method is used to get user details after login. 在CognitoYourUserPoolSample项目中,UserDetailsTableViewController类有一个名为refresh()的方法,此方法用于在登录后获取用户详细信息。 I have added few line of code to identify whether the user confirmed to email address or not. 我添加了几行代码来确定用户是否确认了电子邮件地址。

  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,

}

From email verification view controller call " verifyAttribute " method 从电子邮件验证视图控制器调用“ 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)
}

You need to simply check this value on callback: 您需要在回调时简单地检查此值:

task.result?.userConfirmed?.boolValue

This will return true or false whether user is confirmed or not. 无论用户是否被确认,这都将返回true或false。 Hope this helps. 希望这可以帮助。

As I mentioned above, I haven't had success with either getDetails or getSession for checking if a user has confirmed. 正如我上面提到的,我没有成功使用getDetailsgetSession来检查用户是否已经确认。 What I'm doing instead is calling AWSMobileClient.sharedInstance().signIn . 我正在做的是调用AWSMobileClient.sharedInstance().signIn

If I get an error: AWSMobileClientError.userNotConfirmed , then I take this as indicating the user hasn't confirmed. 如果我收到错误: AWSMobileClientError.userNotConfirmed ,那么我将其视为指示用户尚未确认。

If I successfully get the user signed in, then I take this as indicating the user has confirmed. 如果我成功让用户登录,那么我将其视为用户已确认。

Of course, this has the side effect of leaving the user signed in. I initially was trying to sign the user out after this as part of the signIn callback, but this has the odd side effect of having the signIn callback called twice! 当然,这会留下用户登录的副作用。我最初尝试在此之后签署用户作为signIn回调的一部分,但这有一个奇怪的副作用,即signIn回调被调用两次!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM