Manos Chorianopoulos

fix refreshToken crash

...@@ -932,51 +932,53 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION) ...@@ -932,51 +932,53 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
932 932
933 - (void)refreshToken:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure 933 - (void)refreshToken:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
934 { 934 {
935 - FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"]; 935 + if ([_db tableExists:@"requestVariables"] == YES) {
936 - NSString *refreshToken = [NSString alloc]; 936 + FMResultSet *refreshTokenSet = [_db executeQuery:@"SELECT refresh_token FROM requestVariables WHERE id = 1;"];
937 - while ([refreshTokenSet next]) { 937 + NSString *refreshToken = [NSString alloc];
938 - refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue]; 938 + while ([refreshTokenSet next]) {
939 - } 939 + refreshToken = [[refreshTokenSet resultDictionary][@"refresh_token"] stringValue];
940 - FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"]; 940 + }
941 - NSString *clientId = [NSString alloc]; 941 + FMResultSet *clientIdSet = [_db executeQuery:@"SELECT client_id FROM requestVariables WHERE id = 1;"];
942 - while ([clientIdSet next]) { 942 + NSString *clientId = [NSString alloc];
943 - clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue]; 943 + while ([clientIdSet next]) {
944 - } 944 + clientId = [[clientIdSet resultDictionary][@"client_id"] stringValue];
945 - FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"]; 945 + }
946 - NSString *clientSecret = [NSString alloc]; 946 + FMResultSet *clientSecretSet = [_db executeQuery:@"SELECT client_secret FROM requestVariables WHERE id = 1;"];
947 - while ([clientSecretSet next]) { 947 + NSString *clientSecret = [NSString alloc];
948 - clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue]; 948 + while ([clientSecretSet next]) {
949 - } 949 + clientSecret = [[clientSecretSet resultDictionary][@"client_secret"] stringValue];
950 - 950 + }
951 - NSDictionary *postDictionary3 = @{@"client_id": clientId, @"client_secret": clientSecret, @"refresh_token": refreshToken, @"grant_type":@"refresh_token", }; 951 +
952 - NSData *jsonData3 = [NSJSONSerialization dataWithJSONObject:postDictionary3 options:0 error:NULL]; 952 + NSDictionary *postDictionary3 = @{@"client_id": clientId, @"client_secret": clientSecret, @"refresh_token": refreshToken, @"grant_type":@"refresh_token", };
953 - [self sendContext5:jsonData3 successBlock:^(NSDictionary *contextResponse) { 953 + NSData *jsonData3 = [NSJSONSerialization dataWithJSONObject:postDictionary3 options:0 error:NULL];
954 - if (success) { 954 + [self sendContext5:jsonData3 successBlock:^(NSDictionary *contextResponse) {
955 - [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]]; 955 + if (success) {
956 - NSDictionary *successResponse = @{@"result": @"success", @"status":@1}; 956 + [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [contextResponse objectForKey:@"access_token"], [contextResponse objectForKey:@"refresh_token"]];
957 - success(successResponse); 957 + NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
958 - } 958 + success(successResponse);
959 - } failureBlock:^(NSError *error) { 959 + }
960 - if (failure) {
961 - NSDictionary* dict = [NSDictionary alloc];
962 - dict = [error userInfo];
963 - NSString* errorCode = [dict objectForKey:@"NSLocalizedDescription"];
964 - if ([errorCode isEqual:@"Request failed: unauthorized (401)"]) {
965 - [self refreshToken2ndTry:^(NSDictionary *response) {
966 -// [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [response objectForKey:@"access_token"], [response objectForKey:@"refresh_token"]];
967 - NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
968 - success(successResponse);
969 } failureBlock:^(NSError *error) { 960 } failureBlock:^(NSError *error) {
970 - if (failure) { 961 + if (failure) {
971 - [_db executeUpdate:@"DROP TABLE requestVariables"]; 962 + NSDictionary* dict = [NSDictionary alloc];
972 - failure(error); 963 + dict = [error userInfo];
973 - } 964 + NSString* errorCode = [dict objectForKey:@"NSLocalizedDescription"];
974 - NSLog(@"Error at token %@", error ); 965 + if ([errorCode isEqual:@"Request failed: unauthorized (401)"]) {
966 + [self refreshToken2ndTry:^(NSDictionary *response) {
967 + // [_db executeUpdate:@"UPDATE requestVariables SET access_token = ?, refresh_token = ? WHERE id = 1", [response objectForKey:@"access_token"], [response objectForKey:@"refresh_token"]];
968 + NSDictionary *successResponse = @{@"result": @"success", @"status":@1};
969 + success(successResponse);
970 + } failureBlock:^(NSError *error) {
971 + if (failure) {
972 + [_db executeUpdate:@"DROP TABLE requestVariables"];
973 + failure(error);
974 + }
975 + NSLog(@"Error at token %@", error );
976 + }];
977 + }
978 + NSLog(@"Error at token %@", error );
979 + }
975 }]; 980 }];
976 - } 981 + }
977 - NSLog(@"Error at token %@", error );
978 - }
979 - }];
980 } 982 }
981 983
982 - (void)refreshToken2ndTry:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure 984 - (void)refreshToken2ndTry:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
......