0
votes

I am using Cognito user pool to authenticate users in my system. A successful authentication gives an ID Token (JWT), Access Token (JWT).Every one hour the  TokenExpiration . My question is once my Access Token expires, how do I use the stored refresh token to refresh my access token again?This is my code.

- (void)loginAWSMethod {
    NSString *emailId = @"the email";
    NSString *pwdTxt = @"the password";

    NSLog(@"entered the login method %@ %@",emailId,pwdTxt);
    AWSCognitoIdentityUser *user = [pool getUser:emailId];
    [[user getSession:emailId password:pwdTxt validationData:nil]
     continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task)
     {
         if (task.error) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 dispatch_async(dispatch_get_main_queue(), ^{
                     NSLog(@"ERROR CATCHED++++++");
                     UIAlertController * alert = [UIAlertController
                                                  alertControllerWithTitle:@"Incorrect email or password."
                                                  message:@""
                                                  preferredStyle:UIAlertControllerStyleAlert];

                     UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                                                 {
                                                 }];

                     [alert addAction:yesButton];
                     [self presentViewController:alert animated:YES completion:nil];
                 });

                 [self removeWaitingProgress];
             });

         }else{
             NSLog(@"the result is %@",task.result);
             AWSCognitoIdentityUserSession *response1 = task.result;
             token = response1.accessToken.tokenString;
             NSLog(@"the token is %@",token);
             [[user getDetails] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserGetDetailsResponse *> * _Nonnull task) {
                 AWSCognitoIdentityUserGetDetailsResponse *response = task.result;
                 for (AWSCognitoIdentityUserAttributeType *attribute in response.userAttributes) {
                     //print the user attributes
                     NSLog(@"Attribute: %@ Value: %@", attribute.name, attribute.value);
                     if([attribute.name isEqualToString:@"sub"]){
                         cognitoID = attribute.value;
                     }
                     [defaults setValue:token forKey:@"token"];
                     [defaults setValue:@"yes" forKey:@"isLoggedIn"];
                     [defaults synchronize];
                     dispatch_async(dispatch_get_main_queue(), ^{
                         [self removeWaitingProgress];
                         [self gotoDashborad];
                     });
                 }
                 return nil;
             }];
         }
         return  nil;
     }];
}
1

1 Answers

0
votes

You should be able to simply invoke -[AWSCognitoIdentityUser getSession], which behind the scenes will either return the currently valid access token, or exchange the refresh token for new access token:

-(nullable NSString *)accessTokenStringForCurrentUser {
    AWSCognitoIdentityUser *currentUser = [pool currentUser];
    __block NSString *tokenString;
    // `getSession` automatically exchanges the refresh token for a valid access token if needed
    [[[currentUser getSession] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) {
        // (Error handling not shown)
        if (task.result) {
            AWSCognitoIdentityUserSessionToken *accessToken = task.result.accessToken;
            tokenString = accessToken.tokenString;
        }
        return nil;
    }] waitUntilFinished];
    return tokenString;
}

You may also wish to look at the Cognito UserPools Sample app which has Objective C samples of using UserPools.