2014-01-07 16 views
5

私はDeviseに基づいてモバイルAPIを構築しています。Ruby On Rails Devi Mobile Api

限り、私はJSONレスポンスを必要とするように私が実装したカスタムSessionControllerとRegistrationController:

私のroutes.rbをは次のようになります。工夫でauth_token最後の更新はもう使われていないので、

devise_for(:users, :controllers => { :sessions => "api/sessions", :registrations => "api/registration"}) 

私の知る限り( 3.1.0.rcの下のCHANGELOG.md)。ここで

は私の登録コントローラです:

class Api::RegistrationController < Devise::RegistrationsController 
    respond_to :json 
    skip_before_filter :verify_authenticity_token 

    def create 
    user = User.new(params[:user]) 
    if user.save 
     render :json=> {:auth_token=>user.authentication_token, :email=>user.email}, :status=>201 
     #render :json=> {:success=>true}, :status=>201 
     return 
    else 
     warden.custom_failure! 
     render :json=> user.errors, :status=>422 
    end 
    end 


    protected 

    def invalid_login_attempt 
    render :json=> {:success=>false, :message=>"Error with your login or password"}, :status=>401 
    end 
end 

とセッションコントローラ:

class Api::SessionsController < Devise::SessionsController 
    prepend_before_filter :require_no_authentication, :only => [:create ] 
    before_filter :ensure_params_exist 

    respond_to :json 
    skip_before_filter :verify_authenticity_token 

    def create 
    resource = User.find_for_database_authentication(:email => params[:email]) 
    return invalid_login_attempt unless resource 

    if resource.valid_password?(params[:password]) 
     sign_in("user", resource) 
     resource.ensure_authentication_token 
     render :json=> {:success=>true, :auth_token=>resource.authentication_token, :email=>resource.email} 
     return 
    end 
    invalid_login_attempt 
    end 

    def destroy 
    sign_out(resource_name) 
    end 

    protected 
    def ensure_params_exist 
    return unless params[:email].blank? 
    render :json=>{:success=>false, :message=>"missing user_login parameter"}, :status=>422 
    end 

    def invalid_login_attempt 
    warden.custom_failure! 
    render :json=> {:success=>false, :message=>"Error with your login or password"}, :status=>401 
    end 
end 

私のユーザモデル:

class User < ActiveRecord::Base 
    before_save :ensure_authentication_token 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    attr_accessible :email, :password, :password_confirmation, :remember_me 


    def ensure_authentication_token 
    if authentication_token.blank? 
     self.authentication_token = generate_authentication_token 
    end 
    end 

    private 
    def generate_authentication_token 
    loop do 
     token = Devise.friendly_token 
     break token unless User.where(authentication_token: token).first 
    end 
    end 
end 

あなたは私が使用した、上から見ることができるようにthis question自分の認証トークンを生成する。残念ながら、次のクエリで認証した後、

curl -X POST -d '[email protected]&password=password' http://192.168.178.89:3000/users/sign_in 

トークンを取得しました。しかし、私は保護されたデータを取得しようとすると:

curl -X GET -d 'auth_token:UR-Hespxerzorw9UPK9Z' http://192.168.178.89:3000/api/orders.json 

を私が手:

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

誰でも認証の方法を修正するために私を指すことができますか?ありがとう!そうするには

答えて

2

は、次の手順を行うことができます。

1- ApplicationControllerであるべき、OrdersControllerクラスのスーパークラスからbefore_filter :authenticate_user!を削除します。

2 - あなたのOrdersControllerクラスに以下を追加することで、独自のトークンチェックを作成します。

before_filter :restrict_access 

# Methods not shown 

protected 

def restrict_access 
    authenticate_or_request_with_http_token do |token, options| 
     User.exists?(authentication_token: token) 
    end 
end 

これは探しauthentication_tokenあなたはUserモデルで作成していたし、要求のトークンのパラメータでそれを一致します。トークンがあなたが要求における記号の応答で得たものです

curl -X GET -H 'Authorization: Token token="sdfksdf34heg5Ms"' http://192.168.178.89:3000/api/orders.json 

、3-

は、APIをテストするには、次のよう curlコマンドを使用することができます。