2016-04-11 3 views
0

レールアクションコントローラからDoorkeeperメソッドを認証する機会はありますか?私は自分のアクション( 'show')の認証だけをスキップしたいと思いますが、特定の条件が当てはまる場合、私はapiauthenticateメソッドを呼び出してその仕事をしたいと思っています。したがって、アクション 'ショー'では、まず条件をチェックし、適用されない場合は、api_authenticateをアクティブにする必要があります。私は、api_authenticateを呼び出し、そこで停止するテストを開始しています。しかし何らかの理由でそれが続けられ、それは止まらない。アクションからドアキーパにアクセスするレールコントローラ

これは私のコントローラ

skip_before_action :api_authenticate, only: :show 

def show 
    param! :id, String, required: true 

    post = Services::Posts.find(params[:id]) 

    if post.public 
    @post = post 
    @user_id = nil 
    else 
    api_authenticate 
    ap "it shouldnt get here if user is not logged in" 
    user = current_resource_owner 
    @post = Services::Posts.show(params[:id], user) 
    @user_id = user.identity rescue nil 
    end 
end 

#more actions.... 

のコードであり、これは私が似た何かを実装している私はauthenticateメソッド

class ApiController < ApplicationController 
    protect_from_forgery with: :null_session 

    # Check the user is authenticated 
    before_action :api_authenticate 

    rescue_from ActionController::RoutingError, :with => :route_error 
    rescue_from ::AbstractController::ActionNotFound, :with => :action_error 
    rescue_from Exception, :with => :base_error if Rails.env.production? 

    def api_authenticate 
    doorkeeper_authorize!() 
    end 
end 

答えて

1

を持ってapi_controller.rbです。以下のコードはテストしていませんが、うまくいくはずです。

skip_before_filter :doorkeeper_authorize! , only: :show 

def show 
    param! :id, String, required: true 

    post = Services::Posts.find(params[:id]) 

    if post.public 
    @post = post 
    @user_id = nil 
    else 
    doorkeeper_authorize! 
    ap "it shouldnt get here if user is not logged in" 
    user = current_resource_owner 
    @post = Services::Posts.show(params[:id], user) 
    @user_id = user.identity rescue nil 
    end 
end 

のAPIコントローラ、

class ApiController < ApplicationController 
    protect_from_forgery with: :null_session 

    # Check the user is authenticated 
    before_action :doorkeeper_authorize! 

    rescue_from ActionController::RoutingError, :with => :route_error 
    rescue_from ::AbstractController::ActionNotFound, :with => :action_error 
    rescue_from Exception, :with => :base_error if Rails.env.production? 

    def doorkeeper_unauthorized_render_options(error: nil) 
    response_hash = { status: false, description: error.description, expired: false } 
    response_hash[:expired] = true if error.description == "The access token expired" 
    { json: response_hash } 
    end 

end 

問題が解決しない場合は、showアクションに渡されたのparamsを追加してください。

関連する問題