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

Determine login type using AWS Cognito during resume session (Swift)

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

我已经为用户池(帐户创建和登录)集成了一个 name/password 模式,以及一个 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
}

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

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 的更简洁的方法,但这可行。