diff options
-rw-r--r-- | Juick/APIClient.h | 2 | ||||
-rw-r--r-- | Juick/APIClient.m | 30 | ||||
-rw-r--r-- | Juick/Model/User.h | 2 | ||||
-rw-r--r-- | Juick/Model/User.m | 6 | ||||
-rw-r--r-- | Juick/ViewControllers/DiscoverViewController.m | 6 | ||||
-rw-r--r-- | Juick/ViewControllers/LoginViewController.m | 6 | ||||
-rw-r--r-- | Juick/ViewControllers/MessagesViewController.m | 2 |
7 files changed, 24 insertions, 30 deletions
diff --git a/Juick/APIClient.h b/Juick/APIClient.h index 0f4c9d1..1329868 100644 --- a/Juick/APIClient.h +++ b/Juick/APIClient.h @@ -21,7 +21,7 @@ -(void) postPMToUser:(NSString *)uname text:(NSString *)text result:(void(^)(NSError *))callback; -(void) fetchChats:(void(^)(NSArray *, NSError *))callback; -(void) fetchChatWithUser:(NSString *)uname callback:(void(^)(NSArray *, NSError *))callback; --(void) authenticate:(void(^)(BOOL))callback; +-(void) authenticate:(void(^)(User *, NSError *))callback; -(void) refreshDeviceRegistration:(DeviceRegistration *)registrationData callback:(void(^)(BOOL))callback; -(void) getUserByName:(NSString *) name callback:(void(^)(User *))callback; -(BOOL) isAuthenticated; diff --git a/Juick/APIClient.m b/Juick/APIClient.m index dcc72a8..f637710 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -66,12 +66,9 @@ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"REST Error: %@", error); - NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; - if (statusCode == 401) { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - callback(nil, [[NSError alloc] initWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]); - }]; - } + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(nil, error); + }]; }]; }]; }); @@ -173,18 +170,15 @@ }]; }]; } --(void) authenticate:(void (^)(BOOL))callback { - [self.manager POST:@"post" parameters:nil progress:nil success:nil failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { - NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; - if (statusCode == 400) { - NSString *username = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"]; - NSString *password = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"]; - [self.manager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password]; - callback(YES); - } else { - [self.manager.requestSerializer clearAuthorizationHeader]; - callback(NO); - } +-(void) authenticate:(void (^)(User *user, NSError *error))callback { + [self.manager GET:@"me" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSString *username = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.username"]; + NSString *password = [SAMKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:@"com.juick.password"]; + [self.manager.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password]; + callback([User fromJSON:responseObject], nil); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [self.manager.requestSerializer clearAuthorizationHeader]; + callback(nil, error); }]; } diff --git a/Juick/Model/User.h b/Juick/Model/User.h index 92a6333..6265687 100644 --- a/Juick/Model/User.h +++ b/Juick/Model/User.h @@ -17,6 +17,6 @@ + (BOOL) isAuthenticated; -+ (void) throwUnableToLogin:(UIViewController *)view; ++ (void) throwUnableToLogin:(UIViewController *)view error:(NSError *)error; @end diff --git a/Juick/Model/User.m b/Juick/Model/User.m index 6d5e818..344eb7b 100644 --- a/Juick/Model/User.m +++ b/Juick/Model/User.m @@ -24,10 +24,10 @@ return password != nil; } -+(void) throwUnableToLogin:(UIViewController *)view { ++(void) throwUnableToLogin:(UIViewController *)view error:(NSError *)error { UIAlertController *alert = [UIAlertController new]; - [alert setTitle:@"Error"]; - [alert setMessage:@"Unable to login, check username/password, or network connectivity"]; + [alert setTitle:@"Something went wrong"]; + [alert setMessage:error.localizedDescription]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]]; UIPopoverPresentationController *popover = [alert popoverPresentationController]; if (popover) { diff --git a/Juick/ViewControllers/DiscoverViewController.m b/Juick/ViewControllers/DiscoverViewController.m index edbd152..ea720be 100644 --- a/Juick/ViewControllers/DiscoverViewController.m +++ b/Juick/ViewControllers/DiscoverViewController.m @@ -24,8 +24,8 @@ -(void) viewDidLoad { self.messagesDelegate = self; if ([User isAuthenticated]) { - [[APIClient sharedClient] authenticate:^(BOOL success) { - if (success) { + [[APIClient sharedClient] authenticate:^(User *user, NSError *error) { + if (user) { if ([self.path length] == 0) { self.path = [APIClient feedUrl]; [self.tabBarItem setTitle:@"My feed"]; @@ -34,7 +34,7 @@ [self setShouldScrollToBottomOnRefresh:NO]; [self refreshData]; } else { - [User throwUnableToLogin:self]; + [User throwUnableToLogin:self error:error]; } }]; diff --git a/Juick/ViewControllers/LoginViewController.m b/Juick/ViewControllers/LoginViewController.m index f8cbcc8..de18286 100644 --- a/Juick/ViewControllers/LoginViewController.m +++ b/Juick/ViewControllers/LoginViewController.m @@ -56,8 +56,8 @@ } - (void) doneSignIn { - [[APIClient sharedClient] authenticate:^(BOOL success) { - if (success) { + [[APIClient sharedClient] authenticate:^(User *user, NSError *error) { + if (user) { [[AppDelegate shared] registerForRemoteNotifications]; CATransition* transition = [CATransition animation]; transition.duration = 0.3; @@ -67,7 +67,7 @@ [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; [self.navigationController dismissViewControllerAnimated:NO completion:nil]; } else { - [User throwUnableToLogin:self]; + [User throwUnableToLogin:self error:error]; } }]; } diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index 265df51..9cece2c 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -31,7 +31,7 @@ } [[APIClient sharedClient] pullNextFromPath:self.path params:self.params callback:^(NSArray *next, NSError *err) { if (err) { - [User throwUnableToLogin:self]; + [User throwUnableToLogin:self error:err]; return; } NSArray *newMsgs = next; |