2016-04-11 19 views
0

Bir ray hareket kontrolöründen Doorkeeper yönteminin doğrulanması için bir şans var mı? Sadece eylemlerimden biri için kimlik doğrulamasını atlamak istiyorum ('show') ancak belirli bir koşul varsa apiauthenticate yöntemini işini yapmak için çağırmak istiyorum. Yani eylemde 'gösteri', her şeyden önce bir koşulu kontrol ederim ve eğer geçerli değilse, o zaman api_authenticate'ı aktive etmem gerekiyor. Api_authenticate'ı çağırması ve orada durdurması gereken bir test başlatıyorum. Ama bir sebepten dolayı devam ediyor ve durmuyor. Erişim Kapıcı hareket et Rails Kontrol Cihazı

Bu

benim denetleyicisi

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.... 

ait kodudur Ve bu kimlik doğrulaması yöntemini benzer

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 

cevap

1

Ben uygulayan bir şey var api_controller.rb olduğunu. Aşağıdaki kodu test etmedim, ama işe yaramalı. Sorun devam ederse

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 kontrolör,

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 

, gösteri eyleme geçti params ekleyin.

İlgili konular