Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4619

Uninitialized constant CustomTokenResponse(NameError) and Unauthorized Access for Valid Token

$
0
0

I have been trying to migrate my rails app to api and i just installed doorkeeper gem for authentication and it generates me a token for a valid user. Now i have two problems

  1. It shows unauthorized access in logs when i try to get page even though my access token is correct

  2. I tried to put a custom token response for login and logout because it wasn't showing me any but it is now throwing me an error when i tried to run rails server

I have followed this guide for the process

https://scotch.io/@jiggs/rails-api-doorkeeper-devise

# frozen_string_literal: true#Doorkeeper.rbDoorkeeper.configure do  # Change the ORM that doorkeeper will use (needs plugins)  orm :active_record  # This block will be called to check whether the resource owner is authenticated or not.  resource_owner_authenticator do    # raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"    # Put your resource owner authentication logic here.    # Example implementation:    current_user || warden.authenticate!(:scope => :user)  end   resource_owner_from_credentials do |routes|    user = User.find_for_database_authentication(:email => params[:email])    if user && user.valid_for_authentication? { user.valid_password?(params[:password]) }      user    end  end  skip_authorization do    true  end  Doorkeeper::OAuth::TokenResponse.send :prepend, CustomTokenResponse  Doorkeeper::OAuth::ErrorResponse.send :prepend, CustomTokenErrorResponse  # WWW-Authenticate Realm (default "Doorkeeper").  #  # realm "Doorkeeper"end
# lib/custom_token_response.rbmodule CustomTokenResponse  def body    user_details = User.find(@token.resource_owner_id)    # call original `#body` method and merge its result with the additional data hash       super.merge({           status_code: 200,           message: I18n.t('devise.sessions.signed_in'),           result: user_details       })  endend
# lib/custom_token_error_response.rbmodule CustomTokenErrorResponse  def body    {      status_code: 401,      message: I18n.t('devise.failure.invalid', authentication_keys: User.authentication_keys.join('/')),      result: []    }    # or merge with existing values by    # super.merge({key: value})  endend

1: from /home/rubyians/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in without_bootsnap_cache'/home/rubyians/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:inblock in load_missing_constant': uninitialized constant CustomTokenResponse (NameError)


Viewing all articles
Browse latest Browse all 4619

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>