Devise Token Auth error: Devise.secret_key was not set
Devise Token Auth error: Devise.secret_key was not set
我目前正在使用 Devise Token Auth (https://github.com/lynndylanhurley/devise_token_auth) gem 并且它在开发中运行良好。但是,在我的生产环境中,当我 运行 rake db:migrate
时,出现以下错误:
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)>'
当我添加密钥时,如错误消息所示,出现以下错误:
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)>'
我试过以下方法
- 重新安装 gem
- 设置检查以查看 Devise 配置文件中是否有 Rails.env=="production"
- 更新了 gems
- 使用发电机重新安装设备
- 删除 table 并使用生成器
创建的新迁移进行重新迁移
这是我的 initializers/devise_auth.rb 文件,当我按要求输入密钥时:
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
有什么解决办法吗?为什么这只发生在生产环境中?
根据文档,您需要将 config.secret_key = 'my secret key'
添加到
config/initializers/devise_token_auth.rb
FWIW,您可能不想在代码中保存秘密。使用
config.secret_key = ENV[ 'DEVISE_TOKEN_AUTH_SECRET_KEY' ]
编辑:我认为问题是您需要设置 Devise.secret_key
,而不是 Devise Token Auth 密钥。有 Devise 初始化程序吗?
您正在将其添加到 DeviseTokenAuth
初始值设定项中。相反,创建一个 Devise
初始值设定项 config/initializers/devise.rb
:
Devise.setup do |config|
config.secret_key = '...'
end
注意 1:您可以 运行 rake secret
获取随机密钥并放入其中。
注意 2:有品位的人可能更喜欢在环境变量中保密,以避免将其签入 git:
config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?
我目前正在使用 Devise Token Auth (https://github.com/lynndylanhurley/devise_token_auth) gem 并且它在开发中运行良好。但是,在我的生产环境中,当我 运行 rake db:migrate
时,出现以下错误:
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)>'
当我添加密钥时,如错误消息所示,出现以下错误:
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)>'
我试过以下方法 - 重新安装 gem - 设置检查以查看 Devise 配置文件中是否有 Rails.env=="production" - 更新了 gems - 使用发电机重新安装设备 - 删除 table 并使用生成器
创建的新迁移进行重新迁移这是我的 initializers/devise_auth.rb 文件,当我按要求输入密钥时:
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
有什么解决办法吗?为什么这只发生在生产环境中?
根据文档,您需要将 config.secret_key = 'my secret key'
添加到
config/initializers/devise_token_auth.rb
FWIW,您可能不想在代码中保存秘密。使用
config.secret_key = ENV[ 'DEVISE_TOKEN_AUTH_SECRET_KEY' ]
编辑:我认为问题是您需要设置 Devise.secret_key
,而不是 Devise Token Auth 密钥。有 Devise 初始化程序吗?
您正在将其添加到 DeviseTokenAuth
初始值设定项中。相反,创建一个 Devise
初始值设定项 config/initializers/devise.rb
:
Devise.setup do |config|
config.secret_key = '...'
end
注意 1:您可以 运行 rake secret
获取随机密钥并放入其中。
注意 2:有品位的人可能更喜欢在环境变量中保密,以避免将其签入 git:
config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?