Devise令牌验证错误:Devise.secret_key未设置 [英] Devise Token Auth error: Devise.secret_key was not set

查看:438
本文介绍了Devise令牌验证错误:Devise.secret_key未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Devise令牌认证( https://github.com/lynndylanhurley/devise_token_auth )宝石,并在开发中运作良好。但是,在我的生产环境中,当我运行 rake db:migrate 时,我收到以下错误:

 耙子中止了! 
Devise.secret_key未设置。请将以下内容添加到您的Devise初始化程序中:

config.secret_key ='我的密钥'

请确保在安装Devise或设置密钥后重新启动应用程序。
/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:在`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:在`instance_exec'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set中。 rb:423:在`eval_block'
/Users/karimbutt/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:401:in`绘制'
/Users/karimbutt/Development/projects/haubby/backend/config/routes.rb:1:in`< top(required)>'
/ pre >

当我添加秘密密钥时,如错误消息所示,我收到以下错误:

 耙子中止了! 
NoMethodError:未定义的方法`secret_key ='用于DeviseTokenAuth:$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ top $ top top top top top top top top top top top top top top top top top top top top top top top top top top (必需)>'
/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
- 设置一个检查,看看是否Rails.env ==生产在Devise配置文件
- 更新的gems
- 使用生成器重新安装的设备
- 删除表并重新生成由生成器创建的新迁移



这是我的初始化程序/ devise_auth.rb文件,当我放入密钥,因为它请求:

  DeviseTokenAuth.setup do | config | 
#默认情况下,授权头将在每个请求后更改。
#客户端负责跟踪更改的令牌。将
#this更改为false,以防止每个请求在
#之后更改授权头。
#config.change_headers_on_each_request = true

#默认情况下,用户需要在2周后重新认证。此设置
#确定了令牌在发出后将保持有效的时间。
#config.token_lifespan = 2.weeks

config.secret_key ='我的秘密密钥'

#有时有必要在API上发出多个请求相同的
#时间。在这种情况下,批处理中的每个请求都需要共享相同的
#auth令牌。此设置确定请求可能有多远,而
#仍然使用相同的身份验证令牌。
#config.batch_request_buffer_throttle = 5.seconds

#此路由将是所有oauth2重定向回调的前缀。对于
#示例,使用默认'/ omniauth',github oauth2提供程序将
#redirect成功验证到'/ omniauth / github / callback'
#config.omniauth_prefix =/ omniauth
end

任何想法如何解决?为什么这只是在生产中发生?

解决方案

根据文档,您需要添加 config。 secret_key ='我的密钥'

  config / initializers / devise_token_auth.rb 

FWIW,您可能不想在代码中保存密码。使用

  config.secret_key = ENV ['DEVISE_TOKEN_AUTH_SECRET_KEY'] 

编辑:我认为问题是您需要设置 Devise.secret_key ,而不是设计令牌认证秘密键。有一个Devise初始化程序?


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?

解决方案

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?

这篇关于Devise令牌验证错误:Devise.secret_key未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆