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;
}];
}