在恢复会话期间使用AWS Cognito确定登录类型(Swift)

时间:2017-08-13 02:52:09

标签: swift aws-cognito

我很难在使用AWS Cognito的简历会话期间弄清楚如何确定登录类型。我的代码基于MobileHub示例(如下)。

我已经为用户池(帐户创建和登录)以及Facebook登录按钮集成了名称/密码模式,这些模式都完美无缺。

我的应用程序中有一些逻辑需要根据登录类型而有所不同,但我无法弄清楚如何操作。

有人这样做过吗?

    func didFinishLaunching(_ application: UIApplication, withOptions launchOptions: [AnyHashable: Any]?) -> Bool {
    print("didFinishLaunching:")

    // Register the sign in provider instances with their unique identifier
    AWSSignInManager.sharedInstance().register(signInProvider: AWSFacebookSignInProvider.sharedInstance())
    AWSIdentityProfileManager.sharedInstance().register(FacebookIdentityProfile.sharedInstance(), forProviderKey: AWSFacebookSignInProvider.sharedInstance().identityProviderName)
    AWSSignInManager.sharedInstance().register(signInProvider: AWSCognitoUserPoolsSignInProvider.sharedInstance())
    AWSIdentityProfileManager.sharedInstance().register(UserPoolsIdentityProfile.sharedInstance(), forProviderKey: AWSCognitoUserPoolsSignInProvider.sharedInstance().identityProviderName)

    setupAPIGateway()
    setupS3()

    let didFinishLaunching: Bool = AWSSignInManager.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)

    if (!isInitialized) {
        AWSSignInManager.sharedInstance().resumeSession(completionHandler: { (result: Any?, authState: AWSIdentityManagerAuthState, error: Error?) in
            print("didFinishLaunching Result: \(String(describing: result)) AuthState: \(authState) \n Error:\(String(describing: error))")
            if authState == .authenticated {
                // Facebook or Cognito???
                AWSCognitoUserAuthHelper.getCurrentUserAttribute(name: "sub", completionHandler: { (userid) in
                    // we need to fetch the user
                    ObjectManager.instance.getUser(userid: userid, completionHandler: { (user) in
                        ObjectManager.instance.setCurrentUser(user: user)
                    })
                })
            }
        }) // If you get an EXC_BAD_ACCESS here in iOS Simulator, then do Simulator -> "Reset Content and Settings..."
        // This will clear bad auth tokens stored by other apps with the same bundle ID.
        isInitialized = true
    }

    return didFinishLaunching
}

1 个答案:

答案 0 :(得分:0)

我找到的一个解决方案是转换为不同的身份配置文件类型,如下所示:

let identityManager = AWSIdentityManager.default()

if let fbIdentityProfile = identityManager.identityProfile as? FacebookIdentityProfile {
    print("didFinishLaunching - Facebook login")
} else if let upIdentityProfile = identityManager.identityProfile as? UserPoolsIdentityProfile {
    print("didFinishLaunching - User Pools login")
}

我可以围绕此模型在我的应用程序中建模。不确定是否有更清洁的方法使用MobileHub帮助程序类或AWS API,但这是有效的。