1

我已经在我的应用程序的登录部分工作了一段时间。我正在尝试使用 ASW Mobile Hub 来解决这个问题。我找到了一种让它与我需要的不同提供商一起工作的方法:我自己的用户池、FB 和 Google。

问题是我一直在这里和整个 AWS 文档中搜索,试图找到获取用户数据(用户名和一些其他用户数据,如图片、电子邮件等)的方法。如果我直接使用 FBSDK(使用 FBSDKGraphRequest),我可以得到它,但如果用户选择登录我的 cognito-user-pool,我不知道该怎么做。我也看不到用户成功使用的提供程序。

我可以找到其他一些方法来获得它,但使用旧的 SDK o 直接 Cognito 调用,最初并不是我需要的。这是我用来显示登录窗口的代码:

    override func viewDidLoad() {
       super.viewDidLoad()

       if !AWSSignInManager.sharedInstance().isLoggedIn {
         presentAuthUIViewController()
       }
    }

    func presentAuthUIViewController() {
        let config = AWSAuthUIConfiguration()
        config.enableUserPoolsUI = true
        config.addSignInButtonView(class: AWSFacebookSignInButton.self)
        config.addSignInButtonView(class: AWSGoogleSignInButton.self)


        AWSAuthUIViewController.presentViewController(
        with: self.navigationController!,
        configuration: config, completionHandler: { (provider: 
        AWSSignInProvider, error: Error?) in
            if error == nil {
                // SignIn succeeded.
            } else {
                // end user faced error while loggin in, take any 
                required action here.
            }
        })
     }

那么问题来了,一旦登录成功,如何获取相关的用户信息?

4

2 回答 2

0

如果用户使用 cognito 登录,您可以使用以下代码获取用户名。

let identityManager = AWSIdentityManager.default()
let identityUserName = identityManager.identityProfile?.userName

为了在用户成功后检索提供者,请将其保留在会话中,如下所示

func onLogin(signInProvider: AWSSignInProvider, result: Any?, 
authState: AWSIdentityManagerAuthState, error: Error?) {
  let defaults = UserDefaults.standard
  defaults.set(signInProvider.identityProviderName, forKey: 
     "identityProviderName")
}

希望这个答案有帮助。

更新代码以获取用户名:

let pool = AWSCognitoIdentityUserPool.init(forKey: "CognitoUserPools")
let username = pool.currentUser()?.username
于 2018-02-01T00:35:24.420 回答
0

我一直在研究一种解决方法,直到我以更优雅的方式解决这个问题。我想我需要更深入地了解 Cognito 的理解。但事实上,即使是亚马逊提供的样本也没有显示用户名......

示例亚马逊应用程序屏幕

因此,与此同时,我修改了 Cognito 库的源代码,AWSUserPoolsUIOperations以便将数据直接发送到我的应用程序,并显示一条消息:

@implementation AWSUserPoolsUIOperations

-(void)loginWithUserName:(NSString *)userName
            password:(NSString *)password
            navigationController:(UINavigationController *)navController
            completionHandler:(nonnull void (^)(id _Nullable, NSError * 
            _Nullable))completionHandler {
            self.userName = userName;

NSDictionary* userInfo = @{@"username": self.userName};

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"UsernameNotification"
 object:nil userInfo:userInfo];

然后只是在应用程序中获取消息并存储值。

    @objc private func TestNotification(_ notification: NSNotification){
    if let dict = notification.userInfo as NSDictionary? {
        if let username = dict["username"] as? String {
            appEstats.username = username
            defaults.set(username, forKey: sUserName)
        }
    }
}

正如我所说,这不是解决方案,但同时它可以工作。

于 2018-03-06T07:56:21.590 回答