我有一个基于grape的API,其唯一的客户端是基于浏览器的用户,他们已经通过Devise对我的应用程序进行了身份验证。我希望能够利用一些设计控制器辅助方法,如current_user
,如果没有当前经过身份验证的用户,也可以返回403。有合理的方法吗?
我天真地试图导入控制器助手方法:
class API < Grape::API
prefix 'api'
format :json
include Devise::Controllers::Helpers
get 'hello' do
# would like to be able to use current_user here
{ result: 'hello' }
end
end
current_user
和其他辅助方法仍然无法使用并抛出undefined local variable or method 'current_user'
。
有没有办法利用设计已经完成的认证,并在我的葡萄API方法中使用它?
你试过这个宝石吗? grape_devise - https://github.com/justinm/grape_devise
我有类似的使用设计辅助方法的要求,但我使用devise_token_auth而不是设计api身份验证。使用这个Gem https://github.com/mcordell/grape_devise_token_auth作为葡萄终点的设计助手。
如果您已找到解决方案,请分享答案。