4

I am currently using the Devise Token Auth (https://github.com/lynndylanhurley/devise_token_auth) gem and have it working 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 in the secret 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 have tried the following - Reinstalling the gem - Setting a check to see if Rails.env=="production" in the Devise config file - Updated gems - Reinstalled devise using the generator - Dropping the table and remigrating with new migrations created by generator

Here is my initializers/devise_auth.rb file when I put in the key as it requests:

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's 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 is this happening only in production?

Karim
  • 1,303
  • 1
  • 14
  • 23

2 Answers2

4

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

config/initializers/devise_token_auth.rb

FWIW, you probably don't want to save the secret in your code. Use

config.secret_key = ENV[ 'DEVISE_TOKEN_AUTH_SECRET_KEY' ]

EDIT: I think the issue is that you need to set the Devise.secret_key, not the Devise Token Auth secret key. Is there a Devise initializer?

B Seven
  • 44,484
  • 66
  • 240
  • 385
  • 1
    Thanks! I did put this inside the initializer - edited the question above to include that file as well. Yes, once I do get this up and running, I plan on just using the ENV variable rather than hard coding it in. – Karim May 07 '15 at 02:21
  • 1
    Got it! Looks like I had to create the devise.rb file as well to config that – Karim May 07 '15 at 03:17
1

You're adding it to the DeviseTokenAuth initializer. Instead, create a Devise initializer config/initializers/devise.rb:

Devise.setup do |config|
  config.secret_key = '...'
end

Note 1: you can run rake secret to get a randomized secret key to drop in there.

Note 2: Classy individuals may prefer keeping their secrets in environment variables, to avoid checking them into git:

config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?
Meekohi
  • 10,390
  • 6
  • 49
  • 58