Overriding Doorkeeper refresh token response

292 views Asked by At

I am trying to also send the user details in the refresh token response using Doorkeeper (I am using it on API only). I tried calling the Doorkeeper::TokensController#create method in one of my other controllers, but it does not work. As a workaround, I created a new controller that inherits from Doorkeeper::TokensController and in the create method I try to do render json: { tokens: JSON.parse(super), user: the_instance_i_want }, but this would do render twice, as this method also has a render.

Is there any other way to solve this? I would also want to refresh the user data when refreshing the token.

1

There are 1 answers

0
Vi. On

You should render the same response for first jwt issue and refresh's. Anyway you can override TokenResponse body method:

module Doorkeeper
  module OAuth
    class TokenResponse
      def body
        {
          "access_token" => token.plaintext_token,
          "token_type" => token.token_type,
          "expires_in" => token.expires_in_seconds,
          "refresh_token" => token.plaintext_refresh_token,
          "scope" => token.scopes_string,
          "created_at" => token.created_at.to_i,
          "CUSTOM1" => YOUR_IMPLEMENTATION,
          # custom
          response_code: I18n.t('custom.status_code.ok'),
          response_message: I18n.t('custom.success.default')
        }.reject { |_, value| value.blank? }
      end
    end
  end
end