使用Rails和Devise进行令牌认证

我正在按照这篇优秀的文章来设置我的rails(3.2)API的身份validation部分:
http://blog.joshsoftware.com/2011/12/23/designing-rails-api-using-rabl-and-devise/

我做了以下步骤:

– 增加了对Gemfile的设计

-Enabled设计用户模型并运行所需的迁移

– 我的用户模型是

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 

以及数据库中的token_authenticable(通过迁移)。

-Subclassed RegistrationController:

 class RegistrationsController  resource_name, :recall => " {controller_path}#new") sign_in(resource_name, resource) current_user.reset_authentication_token! respond_with resource, :location => after_sign_in_path_for(resource) end def update super end end 

– 在routes.rb中,我有:

 devise_for :users, :controllers => {:registrations => "registrations"} 

用户创建

我想要以下请求来创建用户并发送回authentification_token:

 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 

我的理解是逻辑应该进入注册控制器的“创建”方法(应该创建用户并同时登录)。 我认为我错了,因为我得到的回信是:

 {"error":"You need to sign in or sign up before continuing."} 

创建和记录新用户的缺失是什么? 是不是POST到users.json映射到RegistrationController #create?

用户登录

此外,我希望以下请求记录用户(一旦检查登录/密码,将他的authentification_token发回给他)

 curl -H "Accept: application/json" -H "Content-type: application/json" -X GET -d '{"user":{"email":"email@gmail.com","password":"pass"}}' 'http://localhost:3000/users.json 

我想逻辑应该在RegistrationController的“更新”方法中,但不是100%肯定。 登录完成后,我将添加令牌validation以保护其他一些模型的创建/查看。

UPDATE

当我发出:

 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 

没有创建和登录用户的原因以及为什么没有返回authentication_token?

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

 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登录, SessionsController.create处理登录。 如何检索authentication_token并在以后将其作为API调用的一部分重用? 另外,如何覆盖SessionsController.create以调用resource.reset_authentication_token!