我尝试通过使用 json 和 ajax 将以下教程用于 rails api 身份验证:http: //jessehowarth.com/devise
它似乎有效,但无论我输入什么登录凭据(现有和不存在),每次我点击提交时它都会失败。
我使用node.js发出请求
var post_data = querystring.stringify({
'email' : 'foo@bar.com',
'password': 'foo',
'remember_me': 1
});
var options = {
host: 'localhost',
path: '/users/sign_in.json',
port: '3000',
method: 'POST'
};
callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = http.request(options, callback);
req.write(post_data);
req.end();
我的设计会话控制器如下所示:
class SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html{ super }
format.json do
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
return sign_in_and_redirect(resource_name, resource)
end
end
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
respond_to do |format|
format.json {render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)}}
format.html {redirect_to root_url}
end
end
def failure
respond_to do |format|
format.json { render:json => {:success => false, :errors => ["Login failed."]} }
end
end
end
我得到的输出:
{"success":false,"errors":["Login failed."]}
对此有何建议?
谢谢!