Gokul595 / api_guard

JWT authentication solution for Rails APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Undefined method error when tyring to refresh tokens

kcsujeet opened this issue · comments

Screenshot from 2019-03-27 09-38-56

full error log

Started POST "/users/tokens" for 127.0.0.1 at 2019-03-27 09:38:06 +0545
Processing by ApiGuard::TokensController#create as */*
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 10], ["LIMIT", 1]]
  ↳ /var/lib/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.2ms)


  
NoMethodError (undefined method `blacklisted_tokens' for #<User:0x00007f144823d850>):
  
activemodel (5.2.2.1) lib/active_model/attribute_methods.rb:430:in `method_missing'
api_guard (0.1.3) lib/api_guard/jwt_auth/blacklist_token.rb:15:in `blacklisted_tokens_for'
api_guard (0.1.3) lib/api_guard/jwt_auth/blacklist_token.rb:21:in `blacklisted?'
api_guard (0.1.3) lib/api_guard/jwt_auth/authentication.rb:65:in `authenticate_token'
api_guard (0.1.3) lib/api_guard/jwt_auth/authentication.rb:24:in `authenticate_and_set_resource'
api_guard (0.1.3) lib/api_guard/jwt_auth/authentication.rb:11:in `method_missing'
api_guard (0.1.3) app/controllers/api_guard/application_controller.rb:4:in `public_send'
api_guard (0.1.3) app/controllers/api_guard/application_controller.rb:4:in `authenticate_resource'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:426:in `block in make_lambda'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:179:in `block (2 levels) in halting_and_conditional'
actionpack (5.2.2.1) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:180:in `block in halting_and_conditional'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:513:in `block in invoke_before'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:513:in `each'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:513:in `invoke_before'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.2.2.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.2.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.2.1) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.2.1) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.2.1) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.2.1) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.2.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.2.1) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.2.1) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.2.1) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.2.1) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.2.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.2.1) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.2.1) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.2.1) lib/action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'
actionpack (5.2.2.1) lib/action_dispatch/routing/mapper.rb:48:in `serve'
actionpack (5.2.2.1) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.2.1) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.2.1) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.2.1) lib/action_dispatch/routing/route_set.rb:840:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
activerecord (5.2.2.1) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.2.1) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.2.1) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.2.1) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.2.1) lib/rails/rack/logger.rb:26:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.2.1) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
rack-cors (1.0.3) lib/rack/cors.rb:95:in `call'
railties (5.2.2.1) lib/rails/engine.rb:524:in `call'
puma (3.12.1) lib/puma/configuration.rb:227:in `call'
puma (3.12.1) lib/puma/server.rb:660:in `handle_request'
puma (3.12.1) lib/puma/server.rb:474:in `process_client'
puma (3.12.1) lib/puma/server.rb:334:in `block in run'
puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'

@kcsujeet Can you please post your User model code?

@Gokul595 here it is

class User < ApplicationRecord
has_secure_password
api_guard_associations refresh_token: 'refresh_tokens', blacklisted_token: 'blacklisted_tokens'
has_many :refresh_tokens, dependent: :delete_all

has_many :advertisements
end

@kcsujeet Please remove blacklisted_token from the api_guard_associations. Use it only if you have blacklisting configured.

@Gokul595 i do have black listing configured. I followed Token Blacklisting . I also have token refreshing configured.
I wanted both blacklisting and token refreshing in my app, that's why I think this gem is an ideal for me. :)

@kcsujeet Then, you need to have an association in the User model for blacklisted token has_many :blacklisted_tokens, dependent: :delete_all.

@Gokul595 yeah that was the issue. thanks for helping out.