Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access current_user from a Doorkeeper authenticated session

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.

like image 521
dysbulic Avatar asked Dec 24 '13 20:12

dysbulic


1 Answers

I have an API that needed to allow authentication via Doorkeeper OR Devise. The following solution allows the main application to use the same endpoints that Oauth clients would use and share the current_user method.

before_action :doorkeeper_authorize!, unless: :user_signed_in?

def current_user
  @current_user ||= if doorkeeper_token
                      User.find(doorkeeper_token.resource_owner_id)
                    else
                      warden.authenticate(scope: :user)
                    end
end
like image 83
KPheasey Avatar answered Sep 22 '22 13:09

KPheasey