diff options
author | Vitaly Takmazov | 2019-03-21 13:48:40 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2019-03-21 14:08:59 +0300 |
commit | 2ddd7f7964d09236b43485c795970038dd1c309f (patch) | |
tree | 74f3aa2d9022542bad2e1450ab374e38a2312f77 /Juick/APIClient.m | |
parent | 6cab755f3a9308991d52b1870bad28af5edb297c (diff) |
New login API
Diffstat (limited to 'Juick/APIClient.m')
-rw-r--r-- | Juick/APIClient.m | 30 |
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); }]; } |