Ruby on rails 基于Rails和Desive的令牌认证

Ruby on rails 基于Rails和Desive的令牌认证,ruby-on-rails,ruby-on-rails-3,authentication,devise,Ruby On Rails,Ruby On Rails 3,Authentication,Devise,我遵循这篇优秀的文章来设置rails(3.2)API的身份验证部分: 我已完成以下步骤: -将设计添加到文件中 -为用户模型启用Desive并运行所需的迁移 -我的用户模型是 class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable devise :token_authe

我遵循这篇优秀的文章来设置rails(3.2)API的身份验证部分:

我已完成以下步骤:

-将设计添加到文件中

-为用户模型启用Desive并运行所需的迁移

-我的用户模型是

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  devise :token_authenticatable

  attr_accessible :email, :authentication_token, :password, :password_confirmation, :remember_me
end
用户创建

我需要以下请求来创建用户并发回authentification_令牌:

curl -H "Accept: application/json" -H "Content-type: application/json"  -X POST -d '{"user":{"email":"email@gmail.com", "password":"pass"}}' 'http://localhost:3000/users.json
我的理解是,逻辑应该放在注册控制器的“create”方法中(该方法应该创建用户并同时让他登录)。我想我应该错了,因为我得到的信息是:

{"error":"You need to sign in or sign up before continuing."}
创建和记录新用户所缺少的部分是什么?POST to users.json是否映射到RegistrationController#create

用户登录

另外,我希望以下请求登录用户(在检查登录/密码后将其身份验证令牌发送回用户)

我想逻辑应该放在RegistrationController的“更新”方法中,但不是100%确定。登录完成后,我将添加令牌身份验证,以保护其他一些模型的创建/视图

更新

当我发布:

curl -H "Accept: application/json" -H "Content-type: application/json"  -X POST -d '{"user":{"email":"email@gmail.com", "password": "mypass", "phone":"1234567890"}}' 'http://localhost:3000/users.json'
我得到了以下信息:

Started POST "/users.json" for 127.0.0.1 at 2012-03-11 20:50:05 +0100
Processing by RegistrationsController#create as JSON
Parameters: {"user"=>{"email"=>"email@gmail.com", , "password"=>"[FILTERED]", "phone"=>"1234567890"}, "registration"=>{"user"=>{"email"=>"email@gmail.com", "password"=>"[FILTERED]", "phone"=>"1234567890"}, "action"=>"create", "controller"=>"registrations", "format"=>"json"}}
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 1ms

你知道为什么没有创建并登录用户,为什么没有返回身份验证令牌吗?

这是我的错,我会更新博客文章。 您需要添加以下代码以在注册控制器中创建用户

if params[:api_key].blank? or params[:api_key] != API_KEY
  render :json => {'errors'=>{'api_key' => 'Invalid'}}.to_json, :status => 401
  return
end
build_resource
if resource.save
   sign_in(resource)
   resource.reset_authentication_token!
   #rabl template with authentication token
   render :template => '/devise/registrations/signed_up' 
else
   render :template => '/devise/registrations/new' #rabl template with errors 
end

如果您有任何问题,请告诉我。

关于吕克的问题,我也理解这一点

例如,使用默认的非API登录,
sessioncontroller.create
处理登录。如何检索
身份验证\u令牌
,并在以后的API调用中重用它?
另外,如何覆盖
sessioncontroller.create
调用
resource.reset\u authentication\u令牌

如果希望在身份验证之前创建用户,则应添加该代码。Desive不行。非常感谢,注册工作正常。关于部分登录,我的理解是要修改的代码在SessionController的“创建”方法中,您确认了吗?在这种情况下,登录名/密码是否已发送以进行身份验证?您需要在sessions_controller#创建方法中的
登录后添加以下行(资源名称,资源)当前\u用户。重置\u身份验证\u令牌
如果您使用的是非api,则可以作为当前\u用户。身份验证\u令牌访问它
curl -H "Accept: application/json" -H "Content-type: application/json"  -X POST -d '{"user":{"email":"email@gmail.com", "password": "mypass", "phone":"1234567890"}}' 'http://localhost:3000/users.json'
Started POST "/users.json" for 127.0.0.1 at 2012-03-11 20:50:05 +0100
Processing by RegistrationsController#create as JSON
Parameters: {"user"=>{"email"=>"email@gmail.com", , "password"=>"[FILTERED]", "phone"=>"1234567890"}, "registration"=>{"user"=>{"email"=>"email@gmail.com", "password"=>"[FILTERED]", "phone"=>"1234567890"}, "action"=>"create", "controller"=>"registrations", "format"=>"json"}}
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 1ms
if params[:api_key].blank? or params[:api_key] != API_KEY
  render :json => {'errors'=>{'api_key' => 'Invalid'}}.to_json, :status => 401
  return
end
build_resource
if resource.save
   sign_in(resource)
   resource.reset_authentication_token!
   #rabl template with authentication token
   render :template => '/devise/registrations/signed_up' 
else
   render :template => '/devise/registrations/new' #rabl template with errors 
end