Add custom TokensController for login

This commit is contained in:
Justin Edmund 2020-09-25 10:42:34 -07:00
parent 7dcd298fc7
commit 2def96352f
2 changed files with 37 additions and 0 deletions

View file

@ -0,0 +1,36 @@
class TokensController < Doorkeeper::TokensController
# Overriding create action
# POST /oauth/token
def create
response = strategy.authorize
body = response.body
if response.status == :ok
# User the resource_owner_id from token to identify the user
user = User.find(response.token.resource_owner_id) rescue nil
unless user.nil?
### If you want to render user with template
### create an ActionController to render out the user
# ac = ActionController::Base.new()
# user_json = ac.render_to_string( template: 'api/users/me', locals: { user: user})
# body[:user] = Oj.load(user_json)
### Or if you want to just append user using 'as_json'
body[:user] = {
id: user.id,
username: user.username
}
end
end
self.headers.merge! response.headers
self.response_body = body.to_json
self.status = response.status
rescue Doorkeeper::Errors::DoorkeeperError => e
handle_token_exception e
end
end

View file

@ -1,5 +1,6 @@
Rails.application.routes.draw do
use_doorkeeper do
controllers :tokens => 'tokens'
skip_controllers :applications, :authorized_applications
end