2013-10-19 11 views
5

アロハ、は[Railsの4]の工夫の上にトークン認証mechanisimローリング

工夫」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

私は問題に遭遇したときにまっすぐに動くこれらのひどい人の一人です。戻って答えを加えるのではなく、私はこれを少し宝石に転がし終わったので、これを過ぎてしまった。 IIRCでは、これは実際のアプリではなくテスト用のenvで問題になりました。私はそれを通って戻って、私がしたことを見つけます。 – user2013350

答えて

0

私はむしろ一から自分を転がすよりも、Authyのdevise moduleトークンベースの認証のための修正/非常に使いやすいを見つけました。

+1

OPはトークン認証を意味し、2つの要素認証を意味しないと思います。 – Han

+0

私は、ソフトトークンやSecureIDなどのハードウェアトークンや認証トークンを意味するトークンを使用しました。訂正してくれてありがとう。 –

関連する問題