4
我正在使用Grape gem为我的应用程序创建一个API。一切工作都很好,直到我将http基本身份验证添加到内置于Grape API中的api中。无法访问http_basic块内的任何Grape :: API方法/对象
下面是代码,我有:
require 'grape'
module MyApp
class API < Grape::API
prefix 'api'
version 'v1'
helpers do
def current_user
@current_user ||= User.authenticate(env)
end
def authenticate!
error!('401 Unauthorized', 401) unless current_user
end
end
resources :projects do
http_basic do |u,p|
authenticate! #=> Results in undefined method `authenticate!' for MyApp::API:Class (NoMethodError)
error! "401 Unauthorized", 401
!current_user.nil?
end
end
end
end
看来我无法访问http_basic块内的任何方法或对象包括请求,ENV,佣工方法中任何东西,甚至没有错误! 。
看代码,这是没有意义的。
有没有人遇到过这个?有没有人有任何使用Grape API和http基本认证的例子?网络上的例子并不是真实世界的例子。
但是,你如何获得认证用户(又名'current_user')? –
医生什么问题也是我的问题。 。 。我想设置一个变量可访问来自http_basic块的API调用,但是这听起来可能不起作用,因为http_basic块最终会在Grape的API类之外创建它。 – brokenbeatnik