I tried to implement all the Cognito SignUp/Confirm/SignIn Stuff in a CognitoController singleton class.
I think there are two functions where my problem probably is based:
The first function is to re-establish a session and is called from my main view controller which receives the callback and then proceeds to initialize the session or shows the signInViewController:
func handleSignInToExistingSession() {
if AWSIdentityManager.default().identityId != nil {
if AWSFacebookSignInProvider.sharedInstance().token().result == nil {
//print("AWSFacebookSignInProvider token: \(String(describing: AWSFacebookSignInProvider.sharedInstance().token().result))")
AWSFacebookSignInProvider.sharedInstance().reloadSession()
//print("AWSFacebookSignInProvider token reload possible: \(String(describing: AWSFacebookSignInProvider.sharedInstance().token().result))")
}
AWSSignInManager.sharedInstance().resumeSession(completionHandler: { (result: Any?, authState: AWSIdentityManagerAuthState, error: Error?) in
DispatchQueue.main.async(execute: {
if error != nil {
self.signInExistingSessionError?(error! as NSError)
} else if result != nil {
self.loginType = AWSFacebookSignInProvider.sharedInstance().isLoggedIn ? "facebook" : AWSCognitoUserPoolsSignInProvider.sharedInstance().isLoggedIn() ? "userpool" : "undefined"
self.signInExistingSessionCompleted?()
}
print("Result: \(String(describing: result)) \n Error:\(String(describing: error))")
})
})
} else {
self.signInExistingSessionError?(nil)
}
}
The second function is to sign in a user and is called from the SignInViewController:
func handleSignInWithSignInProvider(_ signInProvider: AWSSignInProvider) {
AWSSignInManager.sharedInstance().login(signInProviderKey: signInProvider.identityProviderName, completionHandler: {(result: Any?, authState: AWSIdentityManagerAuthState, error: Error?) in
DispatchQueue.main.async(execute: {
if error != nil {
self.signInError?(error! as NSError)
} else {
self.signInCompleted?(true)
}
return
})
//print("result = \(String(describing: result)), error = \(String(describing: error))")
})
}
Everything works fine. But when I logout the session with this function which is called from the mainViewController:
func handleLogout() {
if (AWSSignInManager.sharedInstance().isLoggedIn) {
AWSSignInManager.sharedInstance().logout(completionHandler: {(result: Any?, authState: AWSIdentityManagerAuthState, error: Error?) in
DispatchQueue.main.async(execute: {
self.presentSignedOutViewController()
self.presentLoadingView()
})
//SessionController.sharedInstance.resetSession()
})
// print("Logout Successful: \(signInProvider.getDisplayName)");
} else {
assert(false)
}
}
And try to log back in from the SignInViewController. I receive the message: "Obtaining an identity id in another thread failed or didn't complete within 5 seconds."
The full Message is: Error Domain=com.amazonaws.service.cognitoidentity.AWSCognitoCredentialsProviderHelper Code=0 "Obtaining an identity id in another thread failed or didn't complete within 5 seconds."
Then, if I tap the signInButton another time, it signs in. But the first time after logout the login won't work.
I wasn't able to find any information on this error message that made sense to me.
Thanks to anyone who has an idea how to fix this!