Search code examples
ruby-on-railsheroku

Rails.application.credentials.dig(:secret_key_base) returning nil in Heroku production


I deployed my project on Heroku for production testing.

In development environment I can use Rails.application.credentials.dig(:secret_key_base) to get secret key base for JWT authentication:

    # example
    def decode(token)
      key = Rails.application.credentials.dig(:secret_key_base)
      JWT.decode(token, key)
    end

But when I use that in Heroku it returns "nil". I tried entering it in the console via rails console and it returned "nil".

What do I do to make it available in production?


Solution

  • In order to decrypt the credentials file, you need to have RAILS_MASTER_KEY set in your environment.

    You need to get the value from config/master.key and add it to the environment in Heroku's dashboard.