2013-10-19 13 views
5

अलोहा के शीर्ष पर एक टोकन प्रमाणन mechanisim रोलिंग,वसीयत [रेल 4]

खोज वसीयत 'token_authenticatable मूल्यह्रास कर दिया गया है, मैं अब अपने ही समाधान रोल करने के लिए प्रयास कर रहा हूँ के बाद, हालांकि मुझे लगता है कि मैं एक हो रही है वसीयत के साथ 'sign_in विधि मुद्दा:

कल्पना:

context "with an admin user" do 
    before(:each) { @user = FactoryGirl.create(:user, account_type: 'admin') } 
    it "should respond with a 200 status" do 
     post :verify, "token"=> @user.authentication_token 
     response.status.should eq(200) 
    end 
end 

त्रुटि:

1) UsersController#verify with an admin user should respond with a 200 status 
    Failure/Error: post :verify, "token"=> @user.authentication_token 
    NoMethodError: 
     undefined method `user' for nil:NilClass 
    # ./app/controllers/application_controller.rb:24:in `authenticate_user_from_token!' 
    # ./spec/controllers/users_controller_spec.rb:39:in `block (4 levels) in <top (required)>' 

application_controller.rb:

class ApplicationController < ActionController::Base 
    # If there's a token present we're using the api authentication 
    # mechanism, else we fall back to devise auth 
    before_filter :authenticate_user_from_token!, :authenticate_user! 

    # Setup an AccessDenied error 
    class AccessDenied < StandardError; end 
    # setup a handler 
    rescue_from AccessDenied, :with => :access_denied 


    private 

    # API requests should be made to the resource path 
    # with the requesters token as params. 
    # 
    # This method extracts the params, checks if they are 
    # valid and then signs the user in using devise' sign_in method 

    def authenticate_user_from_token! 
    user = User.find_by_authentication_token params[:token] 

    if !user.nil? && user.admin? 
     # store: false ensures we'll need a token for every api request 
     sign_in user, store: false # this is the line the spec complains about 
    else 
     raise ApplicationController::AccessDenied 
    end 
    end 

    def access_denied 
    render :file => "public/401", :status => :unauthorized 
    end 


end 

users_controller.rb

class UsersController < ApplicationController 

    [snip] 

    # We use this 'verify' method to provide an endpoint 
    # for clients to poll for token verification 
    # If the before filter rejects the user/token 
    # they recieve a 401, else we respond with a 200 
    # and the user params for verification on the remote app 
    def verify 
    user = User.find_by_authentication_token params[:token] 
    render json: user 
    end 
end 

मैं नहीं जानता कि जहां 'उपयोगकर्ता विधि त्रुटि का उल्लेख है बुलाया जा रहा है, और न ही क्या वस्तु उस पर बुलाया जा रहा है।

+0

एक ही समस्या होने के साथ। अगर किसी ने कुछ अंतर्दृष्टि साझा की तो प्यार होगा! –

+0

मैं इन भयानक लोगों में से एक हूं जो वापस आने और जवाब जोड़ने के बजाय, जब मुझे कोई समस्या मिलती है, तो सीधे चलती है। मुझे यह पता चला कि यद्यपि मैंने इसे बहुत छोटे मणि में घुमाया था। आईआईआरसी, वास्तविक ऐप की बजाय परीक्षण एनवी के साथ ऐसा करने में समस्या थी। मैं इसके माध्यम से वापस जाऊंगा और पता लगाया कि मैंने क्या किया .. – user2013350

उत्तर

0

मुझे ऑथी devise module स्क्रैच से अपना खुद को रोल करने के बजाए टोकन आधारित प्रमाणीकरण के लिए उपयोग/संशोधित करना बहुत आसान है।

+1

मुझे लगता है कि ओपी का मतलब है टोकन प्रमाणीकरण, दो कारक प्रमाणीकरण नहीं। – Han

+0

मैं देखता हूं, मैंने हार्डवेयर टोकन या प्रमाणीकरण टोकन जैसे मुलायम टोकन या सिक्योरिड का मतलब टोकन लिया है। सुधारों के लिए धन्यवाद। –

संबंधित मुद्दे