簡體   English   中英

使用Rails和Devise進行令牌認證

[英]Token authentication with Rails and Devise

我正在按照這篇優秀文章來設置我的rails(3.2)API的身份驗證部分:
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 < Devise::RegistrationsController
  def new
    super
  end

  def create
    resource = warden.authenticate!(:scope => 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%肯定。 登錄完成后,我將添加令牌驗證以保護其他一些模型的創建/查看。

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!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM