Cognito isSignedIn latency issue after successful login

455 Views Asked by At

In an iOS application using AWS Cognito.

Inside the didCompleteStepWithError(_ error: Error?) method, I'm having trouble with the isSignedIn property which is not immediately true after a successful login.

When I immediately check for:

userPool.currentUser?.isSignedIn // false

But if I delay the check:

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
    print(userPool.currentUser?.isSignedIn) // true
})

1) Why is the isSignedIn property not immediately true ?

2) How to deal with this if I need to do a getDetails() in order to update the UI ? It is a problem since calling getDetails() with isSignedIn still false will cause the SDK to kick-start the authentication process again.

1

There are 1 best solutions below

0
On

I've come across the same issue and the way that I get passed was this:

Right after signing in, when you have the username and password, in the Cognito delegate method: didCompleteStepWithError, call this method:

- (AWSTask<AWSCognitoIdentityUserSession *> *)getSession:(NSString *)username
                                                password:(NSString *)password
                                          validationData:(nullable NSArray<AWSCognitoIdentityUserAttributeType *> *)validationData;

Code will be something like this:

self.user.getSession(username, password:password).continueWithBlock(block: { (result) -> Any? in
       if let idToken = result.result?.idToken{
           //YOU HAVE THE TOKEN
           if self.user?.isSignedIn ?? false{
               print("user finally signed in")
           }
           else{
               print("something weird happened")
           }
       }
})

for me doing this got the user status correct. Definitely not ideal but it did the job for me.