Devise: Unable to find secret_key in Rails 5.1.6 in asset precompile step

Created on 21 May 2018  ·  10Comments  ·  Source: heartcombo/devise

Environment

  • Ruby 2.5.1
  • Rails 5.1.6
  • Devise 4.4.3 (didn't occur with 4.4.1 but also occured with 4.4.2)

Current behavior

When precompiling the assets with RAILS_ENV=production bundle exec rails assets:precompile, I encounter the following error:

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

  config.secret_key = 'df8931d0d1c106eeebf20555bb7815850cbf648c9b8a52415b80fc65c32d2e0984baff7dec2a401c83db76b5240072ad0f954a1e6ff81bc57e20358e515eb489'

When I look for secret keys in my application, the following values are found. Yet, somehow, Devise doesn't find that secret key at all. The values are posted below:

[1] pry(main)> Rails.application.credentials.secret_key_base
NoMethodError: undefined method `credentials' for #<Tomato::Application:0x00007fe212142de0>
from (pry):1:in `<main>'
[2] pry(main)> Rails.application.secrets.secret_key_base
"e3d3304b39dc13f2fbd50ce6bef4e3635cafd7bc81eb5421976665539b8279fad2c67b9336e2853d8b5104777b8fc9199bd20bf383a0beab291c8cd1cf6c2cb8"
[3] pry(main)>
[4] pry(main)> Rails.application.config.secret_key_base
nil

Expected behavior

Devise wouldn't crash but would use the value from Rails.application.secrets.secret_key_base.

p.s. this is a (very late) follow-up on https://github.com/plataformatec/devise/issues/4807#issuecomment-378259479

Needs debugging

Most helpful comment

Thank you for the hint on fetch. I'll use that in my project. Thanks for the help!

All 10 comments

Hello @cmitz, thanks for your report.
Can you provide us a sample application that reproduces the issue in isolation?
You can also use our bug report template.
That would help us find the issue.

Thank you!

For now, the repository is available at https://github.com/cmitz/devise-4879

Please allow me a couple of days to figure out that bug report template. I've never used that before.

No need to create the bug report template if you already sent the sample repository, we only need one of them.
I'll try to find the issue in your repo.

Thanks!

I'm seeing the same issue with

devise-4.4.3
rails-5.1.6

I see this after running
rake db:migrate RAILS_ENV=production

the database was successfully created with
rake db:create RAILS_ENV=production

I do see the same error:

Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = ''

which adding the secret key to the initializer in devise_token_auth.rb, I get the following error:

NoMethodError: undefined method secret_key= for DeviseTokenAuth:Module

Is this most likely a devise token auth error?

I can also provide a testcase if needed.

@cmitz Sorry for taking so long on this, but I was able to make some tests in your app today, and I saw that you're setting secret_key_base based on an environment variable. If this isn't set, we get the error you mentioned.
But, if I put a literal value in there, eg. secret_key_base: 123, it works as expected. So, I imagine the problem is where you're setting this environment variable, if I recall correctly it was with docker, right?
I recommend you to take a look at https://github.com/bkeepers/dotenv if you haven't yet. Also, since this variable is required for the app to run, it would be good to get its value using #fetch, because an error will be raised if the variable wasn't found. That way will know for sure what is the problem.

secret_key_base: <%= ENV.fetch("SECRET_KEY_BASE") %>
❯ bin/rails c
(erb):4:in `fetch': key not found: "SECRET_KEY_BASE" (KeyError)
    from (erb):4:in `<main>'
    from /Users/tegon/.rbenv/versions/2.5.1/lib/ruby/2.5.0/erb.rb:876:in `eval'

I hope this helps.

@jdevng Since you're using another gem I'm not sure if the error is here or there. But if you can send us a failing test case using our bug report template I can look into it.

Thank you for the hint on fetch. I'll use that in my project. Thanks for the help!

Fixed in v4.5.0

I've upgraded to devise 4.5.0 and I'm still seeing this issue with rails 5.0.7 and ruby 2.4.

I have defined secret_key_base in the secrets.yml and commented out the config.secret_key line in devise.rb. Yet it still complains with Devise.secret_key was not set.

Is anybody else seeing this as well?

Nevermind, it is working as expected.

I'm using capistrano, and when I deploy it compiles locally. I didn't have a secret_key_base set in my secrets.yml for production on my local machine. Thus it couldn't find the secret (I was mistakingly thinking it was looking at the secrets.yml on my server for precompile).

Was this page helpful?
0 / 5 - 0 ratings