devise_token_auth icon indicating copy to clipboard operation
devise_token_auth copied to clipboard

undefined method `[]' for nil:NilClass when changing token lifespan

Open gen1321 opened this issue 5 years ago • 6 comments

When posting issues, please include the following information to speed up the troubleshooting process:

  • Version: 1.1.3

  • Rails Stacktrace:

NoMethodError: undefined method `[]' for nil:NilClass
  from devise_token_auth/concerns/user.rb:166:in `build_auth_header'
  from devise_token_auth/concerns/user.rb:178:in `update_auth_header'
  from devise_token_auth/concerns/user.rb:160:in `create_new_auth_token'
  from graphql_devise/concerns/controller_methods.rb:67:in `set_auth_headers'
  from graphql_devise/mutations/login.rb:17:in `resolve'
  from graphql/schema/resolver.rb:95:in `public_send'
  from graphql/schema/resolver.rb:95:in `block (3 levels) in resolve_with_support'
  from graphql/schema.rb:1867:in `after_lazy'
  from graphql/schema/resolver.rb:83:in `block (2 levels) in resolve_with_support'
  from graphql/schema.rb:1867:in `after_lazy'
  from graphql/schema/resolver.rb:76:in `block in resolve_with_support'
  from graphql/schema.rb:1867:in `after_lazy'
  from graphql/schema/resolver.rb:65:in `resolve_with_support'
  from graphql/schema/field.rb:689:in `public_send'
  from graphql/schema/field.rb:689:in `block in public_send_field'
  from graphql/schema/field.rb:704:in `with_extensions'
  from graphql/schema/field.rb:679:in `public_send_field'
  from graphql/schema/field.rb:525:in `block (2 levels) in resolve_field'
  from graphql/schema.rb:1867:in `after_lazy'
  from graphql/schema/field.rb:515:in `block in resolve_field'
  from graphql/schema.rb:1867:in `after_lazy'
  from graphql/schema/field.rb:509:in `resolve_field'
  from graphql/schema/member/instrumentation.rb:80:in `call'
  from graphql/schema/member/instrumentation.rb:80:in `call'
  from graphql/field.rb:136:in `resolve'
  from graphql/execution/execute.rb:321:in `call'
  from graphql/schema/middleware_chain.rb:49:in `invoke_core'
  from graphql/schema/middleware_chain.rb:38:in `invoke'
  from graphql/execution/execute.rb:129:in `block in resolve_field'
  from graphql/tracing.rb:66:in `block in trace'
  from graphql/tracing.rb:80:in `call_tracers'
  from graphql/tracing.rb:66:in `trace'
  from graphql/execution/execute.rb:128:in `resolve_field'
  from graphql/execution/execute.rb:92:in `block in resolve_selection'
  from graphql/execution/execute.rb:85:in `each'
  from graphql/execution/execute.rb:85:in `resolve_selection'
  from graphql/execution/execute.rb:56:in `block in resolve_root_selection'
  from graphql/tracing.rb:66:in `block in trace'
  from graphql/tracing.rb:80:in `call_tracers'
  from graphql/tracing.rb:66:in `trace'
  from graphql/execution/execute.rb:49:in `resolve_root_selection'
  from graphql/execution/execute.rb:31:in `begin_query'
  from graphql/execution/multiplex.rb:113:in `begin_query'
  from graphql/execution/multiplex.rb:84:in `block in run_as_multiplex'
  from graphql/execution/multiplex.rb:83:in `map'
  from graphql/execution/multiplex.rb:83:in `run_as_multiplex'
  from graphql/execution/multiplex.rb:62:in `block (2 levels) in run_queries'
  from graphql/execution/multiplex.rb:196:in `block in instrument_and_analyze'
  from graphql/execution/instrumentation.rb:29:in `block (2 levels) in apply_instrumenters'
  from graphql/execution/instrumentation.rb:46:in `block (2 levels) in each_query_call_hooks'
  from graphql/execution/instrumentation.rb:41:in `each_query_call_hooks'
  from graphql/execution/instrumentation.rb:45:in `block in each_query_call_hooks'
  from graphql/execution/instrumentation.rb:72:in `call_hooks'
  from graphql/execution/instrumentation.rb:44:in `each_query_call_hooks'
  from graphql/execution/instrumentation.rb:27:in `block in apply_instrumenters'
  from graphql/execution/instrumentation.rb:72:in `call_hooks'
  from graphql/execution/instrumentation.rb:26:in `apply_instrumenters'
  from graphql/execution/multiplex.rb:175:in `instrument_and_analyze'
  from graphql/execution/multiplex.rb:61:in `block in run_queries'
  from graphql/tracing.rb:66:in `block in trace'
  from graphql/tracing.rb:80:in `call_tracers'
  from graphql/tracing.rb:66:in `trace'
  from graphql/execution/multiplex.rb:59:in `run_queries'
  from graphql/execution/multiplex.rb:49:in `run_all'
  from graphql/schema.rb:1557:in `multiplex'
  from graphql/schema.rb:1528:in `execute'
  from graphql_devise/graphql_controller.rb:13:in `auth'
  from action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  from abstract_controller/base.rb:196:in `process_action'
  from action_controller/metal/rendering.rb:30:in `process_action'
  from abstract_controller/callbacks.rb:42:in `block in process_action'
  from active_support/callbacks.rb:112:in `block in run_callbacks'
  from raven/integrations/rails/controller_transaction.rb:7:in `block in included'
  from active_support/callbacks.rb:121:in `instance_exec'
  from active_support/callbacks.rb:121:in `block in run_callbacks'
  from active_support/callbacks.rb:139:in `run_callbacks'
  from abstract_controller/callbacks.rb:41:in `process_action'
  from action_controller/metal/rescue.rb:22:in `process_action'
  from action_controller/metal/instrumentation.rb:33:in `block in process_action'
  from active_support/notifications.rb:180:in `block in instrument'
  from active_support/notifications/instrumenter.rb:24:in `instrument'
  from active_support/notifications.rb:180:in `instrument'
  from action_controller/metal/instrumentation.rb:32:in `process_action'
  from action_controller/metal/params_wrapper.rb:245:in `process_action'
  from active_record/railties/controller_runtime.rb:27:in `process_action'
  from abstract_controller/base.rb:136:in `process'
  from action_view/rendering.rb:39:in `process'
  from action_controller/metal.rb:191:in `dispatch'
  from action_controller/metal.rb:252:in `dispatch'
  from action_dispatch/routing/route_set.rb:51:in `dispatch'
  from action_dispatch/routing/route_set.rb:33:in `serve'
  from action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'
  from action_dispatch/routing/mapper.rb:48:in `serve'
  from action_dispatch/journey/router.rb:49:in `block in serve'
  from action_dispatch/journey/router.rb:32:in `each'
  from action_dispatch/journey/router.rb:32:in `serve'
  from action_dispatch/routing/route_set.rb:837:in `call'
  from warden/manager.rb:36:in `block in call'
  from warden/manager.rb:34:in `catch'
  from warden/manager.rb:34:in `call'
  from rack/tempfile_reaper.rb:15:in `call'
  from rack/etag.rb:25:in `call'
  from rack/conditional_get.rb:38:in `call'
  from rack/head.rb:12:in `call'
  from action_dispatch/http/content_security_policy.rb:18:in `call'
  from rack/session/abstract/id.rb:259:in `context'
  from rack/session/abstract/id.rb:253:in `call'
  from action_dispatch/middleware/cookies.rb:648:in `call'
  from action_dispatch/middleware/callbacks.rb:27:in `block in call'
  from active_support/callbacks.rb:101:in `run_callbacks'
  from action_dispatch/middleware/callbacks.rb:26:in `call'
  from action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
  from action_dispatch/middleware/debug_exceptions.rb:32:in `call'
  from action_dispatch/middleware/show_exceptions.rb:33:in `call'
  from rails/rack/logger.rb:38:in `call_app'
  from rails/rack/logger.rb:26:in `block in call'
  from active_support/tagged_logging.rb:80:in `block in tagged'
  from active_support/tagged_logging.rb:28:in `tagged'
  from active_support/tagged_logging.rb:80:in `tagged'
  from rails/rack/logger.rb:26:in `call'
  from action_dispatch/middleware/remote_ip.rb:81:in `call'
  from action_dispatch/middleware/request_id.rb:27:in `call'
  from rack/method_override.rb:22:in `call'
  from rack/runtime.rb:22:in `call'
  from active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
  from action_dispatch/middleware/executor.rb:14:in `call'
  from rack/sendfile.rb:111:in `call'
  from action_dispatch/middleware/host_authorization.rb:77:in `call'
  from rack/cors.rb:100:in `call'
  from raven/integrations/rack.rb:51:in `call'
  from rails/engine.rb:526:in `call'
  from puma/configuration.rb:228:in `call'
  from puma/server.rb:681:in `handle_request'
  from puma/server.rb:472:in `process_client'
  from puma/server.rb:328:in `block in run'
  from puma/thread_pool.rb:134:in `block in spawn_thread'

When i changed token lifespan from 2.weeks(default) to 8.hours I started to get this error. I fixed it by removing old token, after this everything is working. I don't think that this behavior is correct

gen1321 avatar Feb 29 '20 11:02 gen1321

It just happened the same error in my project

anjoseb121 avatar Mar 11 '20 16:03 anjoseb121

If I make a pr on this will u accept it?

gen1321 avatar Mar 13 '20 10:03 gen1321

I was just about to create an issue about this - Any value under 11 days (I've attempted 263 hours which is one hour less than 11 days) results in the exact same issue.

Here is an issue where it is also referenced (but as part of a different issue).

Update

I've discovered that the issue seems to have to do with already existing tokens.

I've not been able to consistently re-create the issue but I have been able to (seemingly) get rid of the issue by removing all tokens for the user:

user.tokens = {}
user.save!

This being the line where the issue is raised I believe this issue is to do with the fact that the entry for some tokens[client_id] has already been removed and we're calling build_auth_header with an already deleted/removed client_id.

@lynndylanhurley, have you any thoughts on this?

TomasBarry avatar Jun 14 '21 14:06 TomasBarry

Hi, I had the same issue about nil class but I had to call a different method, I did it with create_token

Arielicoms avatar Oct 04 '21 16:10 Arielicoms

user.tokens = {}

Hi Tomas, can i know on which files did you alter the user.tokens = {} ?

djarkobobanov avatar Nov 16 '21 09:11 djarkobobanov

I didn't do it in a specific file, rather in the console while debugging to verify that it fixed the issue.

TomasBarry avatar Nov 16 '21 11:11 TomasBarry