[英]iOS User Login Session via Devise but Auth_token not kept
I am building an iphone app with a rails-backed server. 我正在使用支持Rails的服务器构建iPhone应用程序。 I am using the devise gem. 我正在使用devise宝石。 I am having trouble with user logins on the client-side (everything works on the web side, and even in the terminal with CURL). 我在客户端的用户登录时遇到麻烦(一切都在Web端运行,甚至在具有CURL的终端中也是如此)。 On Xcode I can create a user and I can login. 在Xcode上,我可以创建用户并登录。 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. 然后,我被推送到indexViewController-,在这里我收到一个错误,指出帖子不会加载。 The reason is because on the post_controller.rb I have a 原因是因为在post_controller.rb上,我有一个
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. 问题是,成功登录后生成的auth_token没有被存储并传递给不同的视图。 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: 在indexViewController viewDidLoad方法中,我有:
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 isAuthorized是APIClient中的BOOL,用于检查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? 我猜是在这里我缺少一些auth_token实现? Since it is generated automatically by devise- I am not sure how to tell xcode to remember it. 由于它是由devise自动生成的,因此我不确定如何告诉xcode记住它。 The auth_token is a string that has a column in the user table on the db. auth_token是一个字符串,在数据库的用户表中具有一列。 Should I add auth_token as param to the dictionary that holds the user's email and username? 是否应该将auth_token作为参数添加到包含用户电子邮件和用户名的字典中? Or how do I get the token to persist? 或如何使令牌持久化? Any ideas would be helpful. 任何想法都会有所帮助。
Not being intimately familiar with AFNetworking this is a stab in the dark, but presumably you need to set the token for subsequent requests. 并不是很熟悉AFNetworking,这在黑暗中是一个刺,但是大概您需要为后续请求设置令牌。 Assuming APIClient is a wrapper you've added around AFHTTPClient, here's a quick idea of what that might look like after reviewing the code here - AFHTTPClient . 假设APIClient是您在AFHTTPClient周围添加的包装,这是在这里查看了代码AFAFClient之后的快速想法。
+ (void)loginUser:(NSString *)signature
email:(NSString *)email
password:(NSString *)password
block:(void (^)(User *user))block {
NSDictionary *parameters = @{ @"user": @{ @"email": email,
@"password": password } };
[[APIClient sharedClient] postPath:@"/users/sign_in"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
User *user = [[User alloc] initWithDictionary:responseObject];
// retrieve and save auth token
NSString *token = [responseObject objectForKey:@"authToken"];
[[APIClient sharedClient] setAuthorizationHeaderWithToken:token];
if (block) {
block(user);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (block) {
block(nil);
}
}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.