I'm trying to implement the AWS Cognito in my iOS app by Swift.
I followed the steps shown in the AWS Amplify iOS SDK document, and the Google Sign-in for iOS Guides.
Now I can sign-in a user with google account in my app, and I used the AWSMobileClient.sharedInstance().federatedSignIn() method trying to add the user into my User-Pool & ID-Pool. (I'm not sure that is the correct method to realize this purpose...but the "Federated Signin is OK!!!" line in the program below did print out.)
The problem is that I cannot see any information in my AWS console after the user signed in with a Google account. The nums in my User-Pool & ID-Pool do NOT increase. There is a group for Google users created automatically in my User-Pool, but not any user in it.
Can anybody tell me what I have missed? Thanks!
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()
}
}
Unfortunately, the federatedSignIn()
method name is a bit misleading. It only works with Cognito Identity Pool at the moment as mentioned in the doc :
Currently, the federation feature in the AWSMobileClient supports Cognito Identity Pools only.
I can reproduce the behaviour you're experiencing. Check source code on this commit.
When looking at the Amplify source code, this method is only keeping track of state and registers the token. It returns no error even when you pass an invalid token (I tried with 000)
There is no possibility to get a JWT token neither, this is tracked as a feature request : https://github.com/aws-amplify/aws-sdk-ios/issues/1128
I can think about three workarounds :
AWSCognitoAuth
class instead.The Cognito hosted UI allows to do either a federated SignIn or a Cognito SignIn. The link above is a full working project as example.