summaryrefslogtreecommitdiff
path: root/Juick/Model/User.m
blob: 38e42693221c79ca149ba5e7ce97a2c00b1f4afd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
//  User.m
//  Juick
//
//  Created by Vitaly on 01.02.14.
//  Copyright (c) 2014 com.juick. All rights reserved.
//

#import "User.h"

@import PDKeychainBindingsController;
#import "APIClient.h"

@implementation User

+(BOOL) isAuthenticated {
    return [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil;
}

+(void) throwUnableToLogin:(UIViewController *)view {
    UIAlertController *alert = [UIAlertController new];
    [alert setTitle:@"Error"];
    [alert setMessage:@"Unable to login, check username/password, or network connectivity"];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];
    [view presentViewController:alert animated:YES completion:nil];
}

+(void) checkIsValid:(void (^)(BOOL))callback {
    AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]];
    [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) {
            callback(YES);
        } else {
            [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"" password:@""];
            callback(NO);
        }
    }];}

+(void) get:(NSString *) name callback:(void(^)(User *))callback {
    AFHTTPSessionManager *manager = [APIClient sharedClient].manager;
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil];
    [manager GET:@"/users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        
        callback([User yy_modelWithJSON:[(NSArray *)responseObject firstObject]]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        callback(nil);
    }];
}

@end