From 9d78e7d654c51c22ce4be87efef5bc1f8d5a2d08 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 31 Oct 2019 16:16:48 +0300 Subject: Fix authentication --- Juick/ViewControllers/LoginViewController.m | 77 ++++++++++++++++++----------- 1 file changed, 49 insertions(+), 28 deletions(-) (limited to 'Juick/ViewControllers/LoginViewController.m') diff --git a/Juick/ViewControllers/LoginViewController.m b/Juick/ViewControllers/LoginViewController.m index 55099eb..a0e9b97 100644 --- a/Juick/ViewControllers/LoginViewController.m +++ b/Juick/ViewControllers/LoginViewController.m @@ -8,33 +8,32 @@ #import "LoginViewController.h" #import "User.h" -#import "AppDelegate.h" -#import "APIClient.h" -NSString * const UserSignedInNotificationName = @"UserSignedIn"; +NSString * const UserChangedNotificationName = @"UserSignedIn"; @interface LoginViewController() @property (nonatomic, assign) int paddingValue; +-(void) refreshState; @end @implementation LoginViewController - (void) awakeFromNib { [super awakeFromNib]; - self.title = @"Sign in"; [self.view setBackgroundColor:[UIColor colorNamed:@"Background"]]; - [self.visualEffectView.contentView setBackgroundColor:[UIColor colorNamed:@"Background"]]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelSignIn)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(doneSignIn)]; - self.usernameField.text = [APIClient sharedClient].credential.user; - self.passwordField.text = [APIClient sharedClient].credential.password; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; } +- (void)viewWillAppear:(BOOL)animated { + [self refreshState]; +} + -(void) keyboardDidShow:(NSNotification *)sender { CGRect keyboardRect = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; self.bottomConstraint.constant = keyboardRect.size.height - self.view.safeAreaInsets.bottom + self.paddingValue; @@ -47,32 +46,54 @@ NSString * const UserSignedInNotificationName = @"UserSignedIn"; - (void) cancelSignIn { - CATransition* transition = [CATransition animation]; - transition.duration = 0.3; - transition.type = kCATransitionFade; - transition.subtype = kCATransitionFromTop; - - [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; - [self.navigationController dismissViewControllerAnimated:NO completion:nil]; + [self.navigationController dismissViewControllerAnimated:YES completion:nil]; } - (void) doneSignIn { - [[APIClient sharedClient] setCredential:[NSURLCredential credentialWithUser:self.usernameField.text password:self.passwordField.text persistence:NSURLCredentialPersistenceSynchronizable]]; - [[APIClient sharedClient] authenticate:^(User *user, NSError *error) { - if (user) { - [[NSNotificationCenter defaultCenter] postNotificationName:UserSignedInNotificationName object:user]; + + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [[AppDelegate shared].api authenticateWithUser:self.usernameField.text password:self.passwordField.text callback:^(BOOL success){ + if (success) { + [[NSNotificationCenter defaultCenter] postNotificationName:UserChangedNotificationName object:nil]; [[AppDelegate shared] registerForRemoteNotifications]; - CATransition* transition = [CATransition animation]; - transition.duration = 0.3; - transition.type = kCATransitionFade; - transition.subtype = kCATransitionFromTop; - - [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; - [self.navigationController dismissViewControllerAnimated:NO completion:nil]; - } else { - [User throwUnableToLogin:self error:error]; - [[APIClient sharedClient] setCredential:nil]; + [self.navigationController dismissViewControllerAnimated:YES completion:nil]; } }]; } + +- (void)refreshState { + if ([AppDelegate shared].api.currentUser) { + self.title = @"Profile"; + [self.usernameField setHidden:YES]; + [self.passwordField setHidden:YES]; + [self.currentUser setHidden:NO]; + self.currentUser.text = [AppDelegate shared].api.currentUser.uname; + __weak UIImageView *weakAttach = self.imageView; + [[AppDelegate shared].api fetchImageWithURL:[NSURL URLWithString:[AppDelegate shared].api.currentUser.avatar] callback:^(NSData *data) { + [UIView transitionWithView:weakAttach + duration:0.3 + options:UIViewAnimationOptionTransitionCrossDissolve + animations:^{ + weakAttach.image = [UIImage imageWithData:data]; + } + completion:nil]; + }]; + [self.signOutButton setHidden:NO]; + } else { + self.title = @"Sign in"; + self.imageView.image = [UIImage imageNamed:@"Splash"]; + [self.usernameField setHidden:NO]; + [self.passwordField setHidden:NO]; + [self.currentUser setHidden:YES]; + [self.signOutButton setHidden:YES]; + } +} + +- (IBAction)signoutPressed:(id)sender { + [[AppDelegate shared].api signout]; + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [self.navigationController dismissViewControllerAnimated:YES completion:^{ + [[NSNotificationCenter defaultCenter] postNotificationName:UserChangedNotificationName object:nil]; + }]; +} @end -- cgit v1.2.3