【问题标题】:How to get username from AWS Cognito - Swift如何从 AWS Cognito 获取用户名 - Swift
【发布时间】:2018-09-18 00:31:00
【问题描述】:

问答风格:见下面的答案

如何从使用 Cognito 登录的用户那里获取用户名?

我已完成此操作,并且我的用户已登录,现在该怎么办?

AWSAuthUIViewController.presentViewController(
  with: self.navigationController!,
  configuration: config, completionHandler: { (provider: AWSSignInProvider, error: Error?) in
    if error == nil {          
      //get parameters
      }
    } else {
      print(error as Any)
    }
})

}

【问题讨论】:

    标签: swift amazon-cognito


    【解决方案1】:

    先决条件:

    • 在 MobileHub 中注册的应用
    • MobileHub 中的 Cognito 设置
    • Mobilehub 使用 AWS 开发工具包与 Swift 项目集成

    如果你像我一样,你做到这一点几乎没有困难,现在你被困在试图从登录用户那里获取用户名和其他参数。有很多答案,但到目前为止,我还没有找到一个能让你一路走来的答案。

    我能够从各种来源拼凑起来:

       func getUsername() {
        //to check if user is logged in with Cognito... not sure if this is necessary
        let identityManager = AWSIdentityManager.default()
        let identityProvider = identityManager.credentialsProvider.identityProvider.identityProviderName
    
        if identityProvider == "cognito-identity.amazonaws.com" {
          print("************LOGGED IN WITH COGNITO************")
          let serviceConfiguration = AWSServiceConfiguration(region: .USWest2, credentialsProvider: nil)
          let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "YourClientID", clientSecret: "YourSecretKey", poolId: "YourPoolID")
          AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "YourPoolName (typically formatted as YourAppName_userpoool_MOBILEHUB_12345678")
          let pool = AWSCognitoIdentityUserPool(forKey: "YourPoolName")
          // the following line doesn't seem to be necessary and isn't used so I've commented it out, but it is included in official documentation
          //      let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USWest2, identityPoolId: "YourPoolID", identityProviderManager:pool)          
          if let username = pool.currentUser()?.username {            
            print("Username Retrieved Successfully: \(username)")
          } else {
            print("Error getting username from current user - attempt to get user")
            let user = pool.getUser()            
            let username = user.username
            print("Username: \(username)")
          }
        }
      }
    

    要获取您的 ClientID、Secret Key 和 PoolID,请查看您的 awsconfiguration.json

    要获取您的 PoolName,请登录 MobileHub,然后在您项目的后端,转到用户登录,单击电子邮件和密码,然后单击在 Cognito 中编辑。以下页面将您的池名称设为“YourAppName_userpool_MOBILEHUB_12345678”

    编辑:获取所有属性:

      if let userFromPool = pool.currentUser() {        
        userFromPool.getDetails().continueOnSuccessWith(block: { (task) -> Any? in
          DispatchQueue.main.async {
    
            if let error = task.error as NSError? {
              print("Error getting user attributes from Cognito: \(error)")
            } else {
              let response = task.result
              if let userAttributes = response?.userAttributes {
                print("user attributes found: \(userAttributes)")
                for attribute in userAttributes {
                  if attribute.name == "email" {
                    if let email = attribute.value {
                      print("User Email: \(email)")                      
                    }
                  }
                }
    

    【讨论】:

    • 我无法获取用户属性。 getDetails() 方法没有调用。
    【解决方案2】:

    如果您使用的是 Cognito 用户池,则可以使用:

    import AWSUserPoolsSignIn
    
    AWSCognitoUserPoolsSignInProvider.sharedInstance()
        .getUserPool()
        .currentUser()?
        .username
    

    【讨论】:

    • 谢谢,我在我的项目中尝试了所有这些方法,但它们都没有为我做任何事情
    猜你喜欢
    • 2017-08-12
    • 2018-01-13
    • 1970-01-01
    • 2022-11-22
    • 2022-11-07
    • 2019-10-18
    • 2019-12-23
    • 2021-11-19
    • 2021-03-09
    相关资源
    最近更新 更多