Devise Token Auth error: Devise.secret_key was not set

I am currently using the Devise Token Auth utility ( https://github.com/lynndylanhurley/devise_token_auth ) and doing it well in development. However, in my production environment, when I run rake db:migrate

I get the following error:

rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:

  config.secret_key = 'my secret key'

Please ensure you restarted your application after installing Devise or setting the key.
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/rails/routes.rb:480:in `raise_no_secret_key'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/devise-3.4.1/lib/devise/rails/routes.rb:209:in `devise_for'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/devise_token_auth-0.1.31/lib/devise_token_auth/rails/routes.rb:25:in `mount_devise_token_auth_for'
/Users/karimbutt/Development/projects/haubby/backend/config/routes.rb:3:in `block in <top (required)>'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:423:in `instance_exec'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:423:in `eval_block'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:401:in `draw'
/Users/karimbutt/Development/projects/haubby/backend/config/routes.rb:1:in `<top (required)>'

      

When I add the private key, as the error message suggests, I get the following error:

rake aborted!
NoMethodError: undefined method `secret_key=' for DeviseTokenAuth:Module
/Users/karimbutt/Development/projects/haubby/backend/config/initializers/devise_token_auth.rb:12:in `block in <top (required)>'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/devise_token_auth-0.1.31/lib/devise_token_auth/engine.rb:23:in `setup'
/Users/karimbutt/Development/projects/haubby/backend/config/initializers/devise_token_auth.rb:1:in `<top (required)>'

      

I tried the following - Reinstalling the gem - Setting a check to see if Rails.env == "production" in the Devise config file - Updated gems - Reinstall device using generator - Dropping the table and re-migrating with new migrations generated by the generator

Here is my initializers / devise_auth.rb file when I insert the key when it asks:

DeviseTokenAuth.setup do |config|
  # By default the authorization headers will change after each request. The
  # client is responsible for keeping track of the changing tokens. Change
  # this to false to prevent the Authorization header from changing after
  # each request.
  #config.change_headers_on_each_request = true

  # By default, users will need to re-authenticate after 2 weeks. This setting
  # determines how long tokens will remain valid after they are issued.
  #config.token_lifespan = 2.weeks

  config.secret_key = 'my secret key'

  # Sometimes it necessary to make several requests to the API at the same
  # time. In this case, each request in the batch will need to share the same
  # auth token. This setting determines how far apart the requests can be while
  # still using the same auth token.
  #config.batch_request_buffer_throttle = 5.seconds

  # This route will be the prefix for all oauth2 redirect callbacks. For
  # example, using the default '/omniauth', the github oauth2 provider will
  # redirect successful authentications to '/omniauth/github/callback'
  # config.omniauth_prefix = "/omniauth"
end

      

Any ideas how to fix this? Why does this only happen in production?

+3


source to share


1 answer


As per the docs, you need to add config.secret_key = 'my secret key'

in

config/initializers/devise_token_auth.rb

      

FWIW, you probably don't want to keep a secret in your code. Use



config.secret_key = ENV[ 'DEVISE_TOKEN_AUTH_SECRET_KEY' ]

      

EDIT: I think the problem is that you need to set the private key Devise.secret_key

and not the Autodesk Auten Auth private key. Is there a Devise initializer?

+4


source







All Articles