iOS User Login Session via Devise but Auth_token not kept
I am building an iphone app with a rails-backed server. I am using the
devise gem. I am having trouble with user logins on the client-side
(everything works on the web side, and even in the terminal with CURL). On
Xcode I can create a user and I can login. After logging in
(and recieving this in the log: "User logged in!")
I am then pushed to the indexViewController- and here I receive an error
that the posts don't load. The reason is because on the post_controller.rb
I have a
before_filter :authenticate_user!
preventing the posts from loading. The problem is, that the auth_token
which was generated upon a successful login, is not being stored and
passed along to the different views. So then, in the log I get:
'You need to sign in before continuing.'
As if the first part of what I just explained never happened..
In the indexViewController viewDidLoad method I have:
if (![[APIClient sharedClient] isAuthorized]) {
LoginViewController *loginViewController = [[LoginViewController
alloc] init];
[self.navigationController pushViewController:loginViewController
animated:YES];
}
isAuthorized is a BOOL in the APIClient that checks if userID>0
In the user model this is the code that creates a login session
+ (void)loginUser:(NSString *)signature
email:(NSString *)email
password:(NSString *)password
block:(void (^)(User *user))block
{
NSDictionary *parameters = @{ @"user": @{
// @"signature": signature,
@"email": email,
@"password": password
}
};
[[APIClient sharedClient] postPath:@"/users/sign_in"
parameters:parameters success:^(AFHTTPRequestOperation *operation, id
responseObject) {
User *user = [[User alloc] initWithDictionary:responseObject];
if (block) {
block(user);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (block) {
block(nil);
}
}];
}
I am guessing it is here that I am missing some auth_token implementation?
Since it is generated automatically by devise- I am not sure how to tell
xcode to remember it. The auth_token is a string that has a column in the
user table on the db. Should I add auth_token as param to the dictionary
that holds the user's email and username? Or how do I get the token to
persist? Any ideas would be helpful.
No comments:
Post a Comment