为什么 Google 登录用户没有出现在用户池中?

Why Google Sign-In user doesn't show up in the User Pool?

我正尝试通过 Swift 在我的 iOS 应用程序中实施 AWS Cognito。

我按照 AWS Amplify iOS SDK document, and the Google Sign-in for iOS Guides 中显示的步骤操作。

现在我可以在我的应用程序中使用 google 帐户登录用户,并且我使用 AWSMobileClient.sharedInstance().federatedSignIn() 方法尝试将用户添加到我的用户池中& ID 池。 (我不确定这是实现此目的的正确方法...但下面程序中的 "Federated Signin is OK!!!" 行确实打印出来了。)

问题是用户使用 Google 帐户登录后,我在 AWS 控制台中看不到任何信息。我的用户池和 ID 池中的数字不会增加。在我的用户池中自动创建了一个 Google 用户组,但其中没有任何用户。

谁能告诉我我错过了什么? 谢谢!

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print("\(error.localizedDescription)")
        } else {
            let userId = user.userID                  
            let idToken = user.authentication.idToken
            let fullName = user.profile.name
            let givenName = user.profile.givenName
            let familyName = user.profile.familyName
            let email = user.profile.email
        }
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
              withError error: Error!) {
        print(error.localizedDescription)
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
      return GIDSignIn.sharedInstance().handle(url as URL?,
                                               sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                                               annotation: options[UIApplication.OpenURLOptionsKey.annotation])
  }
}

SignInVC.swift

import UIKit
import AWSMobileClient
import GoogleSignIn

class SignInVC: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        GIDSignIn.sharedInstance()?.delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print("\(error.localizedDescription)")
        } else {
            let idToken = user.authentication.idToken

            AWSMobileClient.sharedInstance().federatedSignIn(providerName: IdentityProvider.google.rawValue, token: idToken!) { (userState, error)  in
                if let error = error {
                    print("Federated Sign In failed: \(error.localizedDescription)")
                }
                else {
                    print("Federated Signin is OK!!!")
                }
            }
        }
    }
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!,
              withError error: Error!) {
        print(error.localizedDescription)
    }

    @IBOutlet weak var googleSignInButton: GIDSignInButton!
    @IBAction func GIDSignInButtontap(_ sender: Any) {
        GIDSignIn.sharedInstance()?.signIn()
    }
    @IBAction func googleSignOutButton(_ sender: Any) {
        GIDSignIn.sharedInstance()?.signOut()
    }
}

不幸的是,federatedSignIn() 方法名称有点误导。如 the doc 中所述,它目前仅适用于 Cognito 身份池:

Currently, the federation feature in the AWSMobileClient supports Cognito Identity Pools only.

我可以重现您遇到的行为。检查源代码 on this commit

When looking at the Amplify source code,此方法仅跟踪状态并注册令牌。 returns 即使你传递了一个无效的令牌也没有错误(我试过 000)

也不可能获得 JWT 令牌,这被跟踪为功能请求:https://github.com/aws-amplify/aws-sdk-ios/issues/1128

我可以考虑三种解决方法:

  • 改用AWSCognitoAuthclass。
  • 使用 Amplify 提供的 UI(仅限 Cognito 用户池身份验证,无联合选项)
  • 使用托管的 Cognito UI(参见 working code here

托管的 Cognito UI 允许进行联合登录或 Cognito 登录。上面的 link 是一个完整的工作项目,例如。