I'm working on Rails API made with RocketPants. For JSON serializing I use active_model_serializers, and for OAuth - Doorkeeper.
The problem is with accessing current_user
helper method in class UserSerializer < ActiveModel::Serializer
. Error:
NameError (undefined local variable or method `request' for #<UserSerializer:0xb5892118>)
current_user
helper uses this snippet:
User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
and doorkeeper_token
is:
def doorkeeper_token
methods = Doorkeeper.configuration.access_token_methods
@token ||= OAuth::Token.authenticate request, *methods
end
So as I find out, there is no request
object accessible in Serializer
. How can I make it accessible? Or should there be other way to implement current_user
?
Thanks in advance
I've honestly never really used this stuff, but from looking at the code and documentation, it appears that
ActiveModel::Serializer
makes thecurrent_user
available from the controller but it does so via thescope
attribute of the serializer. So within the serializer, if you need to check whether thecurrent_user
is an admin, for example, you'd do that like: