How to access current_user from a Doorkeeper authenticated session

dysbulic picture dysbulic · Dec 24, 2013 · Viewed 7.9k times · Source

I have a rails app that I am attempting to sync with an android app. I've successfully set up authentication using Doorkeeper as the server and Oltu as the client.

My app tracks habits which exist as per-user lists. My index method in the habits controller is:

def index
  @habits = current_user.habits
end

When authenticating via Devise this method works, when using Doorkeeper current_user is nil.

Answer

Chris picture Chris · Jan 31, 2014

I would override current_user in your base API controller, something like:

private

def current_user
  @current_user ||= User.find(doorkeeper_token[:resource_owner_id])
end