master
  1#import "LoginViewController.h"
  2#import "CreationsTableViewController.h"
  3
  4@interface LoginViewController ()<UITextFieldDelegate>
  5@property (strong, nonatomic) MBProgressHUD *HUD;
  6@end
  7
  8@implementation LoginViewController
  9
 10- (void)viewDidLoad
 11{
 12  [super viewDidLoad];
 13
 14  // prepopulate username/password with stored information
 15  NSError *error;
 16  NSString *username = [SSKeychain passwordForService:KEYCHAIN_USER_NAME account:KEYCHAIN_ACCOUNT error:&error];
 17  if (error) { username = nil; }
 18  NSString *password = [SSKeychain passwordForService:KEYCHAIN_USER_PASSWORD account:KEYCHAIN_ACCOUNT error:&error];
 19  if (error) { password = nil; }
 20
 21  if (username)
 22  {
 23    self.emailTextBox.text = username;
 24  }
 25  if (password)
 26  {
 27    self.passwordTextBox.text = password;
 28  }
 29}
 30
 31- (MBProgressHUD *)HUD
 32{
 33  if (!_HUD) {
 34    _HUD = [[MBProgressHUD alloc] initWithView:self.view];
 35    [self.view addSubview:_HUD];
 36  }
 37  return _HUD;
 38}
 39
 40- (void)didReceiveMemoryWarning
 41{
 42    [super didReceiveMemoryWarning];
 43}
 44
 45#pragma mark UITextFieldDelegate
 46
 47- (BOOL)textFieldShouldReturn:(UITextField *)textField
 48{
 49  // user did hit the Return/Done button on the keyboard
 50  if (textField == self.emailTextBox)
 51  {
 52    // goto password field
 53    [self.passwordTextBox becomeFirstResponder];
 54  }
 55  else
 56    if (textField == self.passwordTextBox)
 57    {
 58      [self login:self.passwordTextBox];
 59    }
 60  return YES;
 61}
 62
 63- (void)textFieldDidEndEditing:(UITextField *)textField
 64{
 65  [textField resignFirstResponder];
 66}
 67
 68- (void)dismissKeyboard
 69{
 70  [self.view endEditing:YES];
 71}
 72
 73#pragma mark - Action methods
 74
 75- (IBAction)backgroundTapped:(id)sender
 76{
 77  [self dismissKeyboard];
 78}
 79
 80- (IBAction)login:(id)sender
 81{
 82  [self dismissKeyboard];
 83  self.HUD.mode = MBProgressHUDModeIndeterminate;
 84  self.HUD.labelText = nil;
 85  self.HUD.customView = nil;
 86  [self.HUD show:YES];
 87
 88  // try to login
 89  NSDictionary *params = @{@"email": self.emailTextBox.text, @"password": self.passwordTextBox.text};
 90  AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST]];
 91  [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
 92  [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
 93  [httpClient.operationQueue setMaxConcurrentOperationCount:1];
 94  [httpClient setParameterEncoding:AFJSONParameterEncoding];
 95  NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:URL_LOGIN parameters:params];
 96
 97  NSLog(@"POST: %@", request);
 98  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
 99     NSLog(@"%@", JSON);
100     // check if login was successfull
101     NSString *token = [JSON objectForKey:@"auth_token"];
102     if (!token || [token isEmpty])
103     {
104       [self.HUD hide:YES];
105       [TSMessage showNotificationInViewController:self withTitle:@"Login failed!" withMessage:@"Please verify your login credentials and try again." withType:TSMessageNotificationTypeError withDuration:OVERLAY_MESSAGE_DURATION];
106     }
107     else
108     {
109       // successfull login!
110       [SSKeychain setPassword:token forService:KEYCHAIN_API_TOKEN account:KEYCHAIN_ACCOUNT];
111       [SSKeychain setPassword:self.emailTextBox.text forService:KEYCHAIN_USER_NAME account:KEYCHAIN_ACCOUNT];
112       [SSKeychain setPassword:self.passwordTextBox.text forService:KEYCHAIN_USER_PASSWORD account:KEYCHAIN_ACCOUNT];
113       CreationsTableViewController *controller = [[CreationsTableViewController alloc] initWithNibName:@"CreationsTableViewController" bundle:nil];
114       [self.navigationController pushViewController:controller animated:YES];
115
116     }
117   }
118   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
119   {
120     [self.HUD hide:YES];
121     NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
122     BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Something's not working." message:@"Login/Signup currently not possible. Please try again."];
123     [alert setCancelButtonWithTitle:@"Ok" block:nil];
124     [alert show];
125   }];
126  [operation start];
127}
128
129@end