diff options
author | 2014-02-02 00:53:45 +0400 | |
---|---|---|
committer | 2014-02-02 00:53:45 +0400 | |
commit | d52c86da9751f221a24c21ec903656e279ba885a (patch) | |
tree | 1b5b8d45efe472c5e997a4ae1f2293d55291f31d /Juick/User.m | |
parent | 552680443dea2ea5e3ce64d03dc4b6d2e39a7226 (diff) |
Login info in reveal panel and login flow improvements (not complete)
Diffstat (limited to 'Juick/User.m')
-rw-r--r-- | Juick/User.m | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Juick/User.m b/Juick/User.m new file mode 100644 index 0000000..6f1678a --- /dev/null +++ b/Juick/User.m @@ -0,0 +1,67 @@ +// +// User.m +// Juick +// +// Created by Vitaly on 01.02.14. +// Copyright (c) 2014 com.juick. All rights reserved. +// + +#import "User.h" +#import "PDKeychainBindings.h" + +@implementation User + ++(BOOL) isAuthenticated { + return [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil; +} + ++(void) throwUnableToLogin { + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to login, check username/password, or network connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; + [alert show]; +} + ++(void) checkIsValid:(void (^)(BOOL))callback { + RKObjectManager *manager = [RKObjectManager sharedManager]; + [manager.HTTPClient setAuthorizationHeaderWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodPOST path:@"/post" parameters:nil]; + RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; + + RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; + + RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request + responseDescriptors:@[responseDescriptor]]; + [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { + // this will never happens + } failure:^(RKObjectRequestOperation *operation, NSError *error) { + if (operation.HTTPRequestOperation.response.statusCode == 400) { + callback(YES); + } else { + callback(NO); + } + }]; + [operation start]; + +} + ++(void) get:(NSString *) name callback:(void(^)(User *))callback { + RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; + [mapping addAttributeMappingsFromDictionary:@ + { + @"uname": @"uname", + @"uid" : @"uid", + }]; + NSString *path = @"/users"; + NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; + RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; + NSURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil method:RKRequestMethodGET path:path parameters:params]; + RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request + responseDescriptors:@[responseDescriptor]]; + [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { + callback([[mappingResult array] firstObject]); + } failure:^(RKObjectRequestOperation *operation, NSError *error) { + NSLog(@"REST Error: %@", error); + }]; + [operation start]; +} + +@end |