Reputation: 1449
I'm using the Drupal iOS SDK to log users into my app. It works great. That said, sometimes my users don't "log out", and just close the app. When they do this, the session doesn't expire; so when they restart the app and try and log in by typing in their username and password, they can't because Drupal tells them they are already logged in.
If a session is still valid, how would I go about writing a line of code that checks the session upon loading the login view, that basically states, "If user is already logged in, go to [Name of ViewController Here]" ?
I thought it would look something like this:
- (void)viewDidLoad {
[super viewDidLoad];
DIOSSession *session = [DIOSSession sharedSession];
//If you have logged in, this object is not nil
[session user];
if ([session user]!= nil) {
// User login displayed
} else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
[self.navigationController pushViewController:yourViewController animated:YES];
}
My login code currently looks like this:
ViewController.m
- (IBAction)loginButton:(UIButton *)sender {
self.activityIndicatorViewOne.hidden = NO;
[self.activityIndicatorViewOne startAnimating];
[DIOSUser
userLoginWithUsername:_userField.text
andPassword:_passField.text
success:^(AFHTTPRequestOperation *op, id response) {
wrongLogin.hidden = YES;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
[self.navigationController pushViewController:yourViewController animated:YES];
[self.activityIndicatorViewOne stopAnimating];
self.activityIndicatorViewOne.hidden = YES;
NSLog(@"Success!");}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); wrongLogin.hidden = NO; }
];
}
Logout method
-(void)flipView {
[DIOSUser
userLogoutWithSuccessBlock:^(AFHTTPRequestOperation *op, id response) { NSLog(@"Success!");
//Pop back to the root view controller
[self.navigationController popToRootViewControllerAnimated:NO];
//Allocate and init the new view controller to push to
ViewController *newVC = [[ViewController alloc] init];
//Push the new view controller
[self.navigationController pushViewController:newVC animated:YES];
}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); }
];
}
Upvotes: 1
Views: 1165
Reputation: 4854
You don't have session after closing/opening the app because SDK doesn't store the user session and CSRF token in some way that it would stay in the phone even after closing it. In iOS you have few ways of doing it. You have NSUserDefaults which is the fastest way, but it is kinda insecure. Then you have Keychain, which would be the best choice here (secure & is used for that type of thing like sessions & password) - but is not that easy to implement. The other option could be Core Data but it is too big for that small task. I will show you how to do it in NSUserDefaults, since I don't know how you store your keychain items (if you don't, I suggest checking out UICKeychainStore).
So the idea here would be to save session & token when your user is logged in. Then clear it when the user logged out. Then, when your user will get to the login page, you can be sure that if it have the session, you can show him the my account view controller.
It would be something like this (Not tested):
Login:
[DIOSUser userLoginWithUsername:_userField.text
andPassword:_passField.text
success:^(AFHTTPRequestOperation *op, id response) {
// Saving to keychain/NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
forKey:@"diosSession"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[DIOSSession sharedSession] getCSRFTokenWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *csrfToken = [NSString stringWithUTF8String:[responseObject bytes]];
[[NSUserDefaults standardUserDefaults] setObject:csrfToken forKey:@"diosToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure handler
}];
wrongLogin.hidden = YES;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
[self.navigationController pushViewController:yourViewController animated:YES];
[self.activityIndicatorViewOne stopAnimating];
self.activityIndicatorViewOne.hidden = YES;
NSLog(@"Success!");}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); wrongLogin.hidden = NO; }
];
Logout:
[DIOSUser userLogoutWithSuccessBlock:^(AFHTTPRequestOperation *op, id response) { NSLog(@"Success!");
// Remove the keys
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"diosSession"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"diosToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Pop back to the root view controller
[self.navigationController popToRootViewControllerAnimated:NO];
//Allocate and init the new view controller to push to
ViewController *newVC = [[ViewController alloc] init];
//Push the new view controller
[self.navigationController pushViewController:newVC animated:YES];
}
failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); }
];
Checking if user was logged in:
NSDictionary *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"];
NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"diosToken"];
if (user && token) {
// Logged in, set the old session & token
[[DIOSSession sharedSession] setUser:user];
[[DIOSSession sharedSession] setCsrfToken:token];
// Push view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
[self.navigationController pushViewController:yourViewController animated:YES];
} else {
// Not logged in, show form
}
Upvotes: 1