summaryrefslogtreecommitdiff
path: root/Volta/Views/LoginViewController.m
blob: 4ae586257b2765ab00890440b91f174f18a49e2c (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
//  LoginViewController.m
//  Volta
//
//  Created by Ronny Fenrich on 2013-06-13.
//  Copyright (c) 2013 Decoder. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *email;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (strong, nonatomic) MBProgressHUD *HUD;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *logo;
@end

@implementation LoginViewController



- (void)viewDidLoad
{
    [super viewDidLoad];


    UIView *emailPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    self.email.leftView = emailPaddingView;
    self.email.leftViewMode = UITextFieldViewModeAlways;

    UIView *passwordPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    self.password.leftView = passwordPaddingView;
    self.password.leftViewMode = UITextFieldViewModeAlways;

    self.contentView.$y = 0;

    // prepopulate username/password with stored information
    NSError *error;
    NSString *username = [SSKeychain passwordForService:KEYCHAIN_USER_NAME account:KEYCHAIN_ACCOUNT error:&error];
    if (error) { username = nil; }
    NSString *password = [SSKeychain passwordForService:KEYCHAIN_USER_PASSWORD account:KEYCHAIN_ACCOUNT error:&error];
    if (error) { password = nil; }

    if (username)
    {
        self.email.text = username;
    }
    if (password)
    {
        self.password.text = password;
    }
}


- (void)viewDidUnload
{
    [self setEmail:nil];
    [self setPassword:nil];
    [self setContentView:nil];
    [self setLogo:nil];
    [super viewDidUnload];
}

- (MBProgressHUD *)HUD
{
    if (!_HUD) {
        _HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:_HUD];
    }
    return _HUD;
}


#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // user did hit the Return/Done button on the keyboard
    if (textField == self.email)
    {
        // goto password field
        [self.password becomeFirstResponder];
    }
    else
        if (textField == self.password)
        {
            [self login:self.password];
        }

    return YES;
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:HUD_ANIMATION_DURATION animations:^{

        self.contentView.$y = (-1) * self.logo.$height - 10;

    }];
}


- (void)dismissKeyboard
{
    [self.view endEditing:YES];
    if (self.contentView.$y!=0)
    {
        [UIView animateWithDuration:HUD_ANIMATION_DURATION animations:^{
            self.contentView.$y = 0;
        }];
    }
}


#pragma mark - Action methods

- (IBAction)dismissKeyboard:(id)sender
{
    [self dismissKeyboard];
}

- (IBAction)backgroundTapped:(id)sender
{
    [self dismissKeyboard];
}

- (IBAction)login:(id)sender
{
//    [self performSegueWithIdentifier:@"gotoMainInterface" sender:nil];
//    return;

    [self dismissKeyboard];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    self.HUD.labelText = nil;
    self.HUD.customView = nil;
    [self.HUD show:YES];


    // try to login/register...
    NSDictionary *params = @{@"email": self.email.text, @"password": self.password.text};

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST]];
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
    [httpClient.operationQueue setMaxConcurrentOperationCount:1];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:URL_LOGIN parameters:params];

    NSLog(@"POST: %@", request);

    AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                         JSONRequestOperationWithRequest:request
                                         success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                         {
                                             NSLog(@"%@", JSON);

                                             // check if login was successfull
                                             NSString *token = [JSON objectForKey:@"auth_token"];
                                             if (!token || [token isEmpty])
                                             {
                                                 [self.HUD hide:YES];
                                                 [TSMessage showNotificationInViewController:self
                                                                                   withTitle:@"Login failed!"
                                                                                 withMessage:@"Please verify your login credentials and try again."
                                                                                    withType:TSMessageNotificationTypeError
                                                                                withDuration:OVERLAY_MESSAGE_DURATION
                                                  ];
                                             }
                                             else
                                             {
                                                 // successfull login!
                                                 [SSKeychain setPassword:token forService:KEYCHAIN_API_TOKEN account:KEYCHAIN_ACCOUNT];
                                                 [SSKeychain setPassword:self.email.text forService:KEYCHAIN_USER_NAME account:KEYCHAIN_ACCOUNT];
                                                 [SSKeychain setPassword:self.password.text forService:KEYCHAIN_USER_PASSWORD account:KEYCHAIN_ACCOUNT];

                                                 [self performSegueWithIdentifier:@"gotoMainInterface" sender:nil];
                                             }

                                         }
                                         failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                         {
                                             [self.HUD hide:YES];
                                             NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);

                                             BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Something's not working." message:@"Login/Signup currently not possible. Please try again."];
                                             [alert setCancelButtonWithTitle:@"Ok" block:nil];
                                             [alert show];
                                         }];
    [operation start];
    
}



@end