summaryrefslogtreecommitdiff
path: root/Juick/APIClient.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/APIClient.m')
-rw-r--r--Juick/APIClient.m30
1 files changed, 12 insertions, 18 deletions
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);
}];
}