如何使用AWS Mobile-hub SDK获取登录用户名

问题描述 投票:1回答:2

我已经在我的应用程序的登录部分工作了一段时间。我正在尝试使用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.
            }
        })
     }

所以,问题是,一旦登录成功,我怎样才能获得相关的用户信息?

facebook amazon-web-services facebook-login aws-mobilehub
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

0
投票

我一直致力于解决方法,直到我以更优雅的方式解决这个问题。我想我需要深入了解Cognito的理解。但事实上甚至亚马逊提供的样本都没有显示用户名...

Sample Amazon app screen

所以,与此同时,我修改了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)
        }
    }
}

正如我所说的不是解决方案,但与此同时它起作用。

© www.soinside.com 2019 - 2024. All rights reserved.