Devise 令牌身份验证错误:未设置 Devise.secret_key

2024-03-22

我目前正在使用 Devise 令牌身份验证 (https://github.com/lynndylanhurley/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)>'

我尝试过以下方法 - 重新安装宝石 - 设置检查以查看 Devise 配置文件中是否有 Rails.env==“生产” - 更新了宝石 - 使用生成器重新安装设备 - 删除表并使用生成器创建的新迁移进行重新迁移

这是我按照要求输入密钥时的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' to

config/initializers/devise_token_auth.rb

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

config.secret_key = ENV[ 'DEVISE_TOKEN_AUTH_SECRET_KEY' ]

编辑:我认为问题是你需要设置Devise.secret_key,而不是 Devise Token Auth 密钥。有 Devise 初始化程序吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Devise 令牌身份验证错误:未设置 Devise.secret_key 的相关文章

  • 在 Windows 7 x64 上的任何源中均找不到 linecache19-0.5.13

    我安装了 Ruby 如果我运行 ruby v I see ruby 1 9 3p125 2012 02 16 i386 mingw32 如果我在我的应用程序目录中运行 rails server output 31mCould not fin
  • App Engine Cron 作业始终返回 HTTP 状态代码 301

    我已关注本指南 https cloud google com appengine docs flexible ruby scheduling jobs with cron yaml为我的 Rails 应用程序创建 cron 作业 但 HTT
  • gsub的时间复杂度

    一根长绳子s仅包含0 and 1 这段 Ruby 代码计算了有多少个1有 s gsub 1 count Big O 表示法的时间复杂度是多少 有没有一个工具可以进行计算 据我所知 没有一个通用工具可以计算任意代码的 Big O 表示法 这将
  • 如何在rails中使用bootstrap Modal和link_to?

    我之前就问过这个问题 但不幸的是我仍然被困住了 我有一个这样的 link to 在 page html erb 当点击链接时我想以模式加载的页面 我有 div class modal Test Content div 在资产 page js
  • 重用named_scope来定义另一个named_scope

    我所看到的问题本质 有一天 如果我没有记错的话 我看到了一个重用一个named scope来定义另一个named scope的例子 像这样的东西 不记得确切的语法 但这正是我的问题 named scope billable conditio
  • Rails - 从对象哈希创建选择标签

    我需要根据哈希中的可用值创建一个选择框 例如 我有一个 事物 该 事物 有多种状态字段 1 gt State A 2 gt State B 通过事物上的方法可用 我如何从中构建一个选择标签 正如施罗克韦尔所说 Hash each a 返回以
  • Jasmine 不会从资源管道加载 javascript 文件

    我正在开发一个 gem 并且我已经安装了 Jasminehttps github com pivotal jasmine gem https github com pivotal jasmine gem 我所需的所有 JS 文件都位于 ap
  • 获取 Devise 身份验证以发送电子邮件以检索密码

    我正在本地主机上测试 Rails 的 Devise 身份验证 并希望让它发送电子邮件以检索密码 即 忘记密码 链接 密码检索内置于 Devise 中 只需正确配置它即可发送电子邮件 在initializers devise rb中 我把 c
  • Rails 使用驼峰命名法渲染 json 对象

    我在简单的 Rails API 中有以下控制器代码 class Api V1 AccountsController lt ApplicationController def index render json Account all end
  • RAILS 3 模型验证包含不起作用

    Schema Information Schema version Table name terms id integer not null primary key user id integer term type string 255
  • 在 Ruby 中,有没有办法重载初始化构造函数?

    在 Java 中你可以重载构造函数 public Person String name this name name public Person String firstName String lastName this firstName
  • Rails 应用程序助手不支持中文字符

    def top cateogries with home category id 1 concat raw
  • Rails 6 + Capistrano - 没有这样的 puma.sock 文件

    拜托 我有一个大问题10多个小时了 每当我使用 Capistrano 和 Puma 在 Rails 中运行应用程序部署并重新启动 nginx 时 当我尝试访问我的 Web 时 我会看到此错误 在此输入图像描述 https i stack i
  • Rails 数据库默认值和布尔字段的模型验证

    在 Rails 模型中我有一个属性is subscriber 当我构建数据库迁移以将此列添加到数据库时 我指定默认值为 false t boolean is subscriber default gt false 我还在模型中指定了该属性需
  • 保护客户端 API 的安全

    我正在为基于 JavaScript 的游戏构建服务器端 API 和客户端库 其中必须确保两个非常重要的功能的安全 用户每次游玩都必须扣款 我们必须确保提交的分数是玩家实际获得的分数 解决第一个问题看起来很简单 在每次游戏开始时 我们都会调用
  • ruby - 数组元素之间的排列

    我正在使用 ruby 在 Google Sketchup 中编写一个插件 在尝试排列数组中存在的两个数组时遇到了一个真正的问题 所有这些都取决于用户组合 我有一个数组数组 例如 1 lol so 当我们有这样的组合 1 2 3 没关系 它应
  • Rails form_for :remote=>true 没有调用 js 方法

    我不知道为什么这不起作用 我正在学习 Rails 我正在关注一本书 它说要这样做 div class field div div class field div
  • 文件上传字段导致 ActionController::InvalidAuthenticityToken 异常

    使用 Rails 4 并尝试使用 simple form 和回形针将文件字段添加到现有表单 这是表格的关键部分 一切正常 除非我实际提交带有上传文件的表单 然后 我得到这个 ActionController InvalidAuthentic
  • 在 Ruby 中定义元编程的方法参数

    在 Ruby 中 我们可以使用元编程来定义实例方法 例如 define method hi Hello SO world gt hi hi gt Hello SO world 这样 就可以定义一个带有动态名称 dynamic name hi
  • ruby on Rails 中的自动测试错误

    我运行了命令自动测试 这是我得到的错误 我正在关注 Hartl 的书 想知道这种冲突是否会发生 因为 Rails 现在附带了 ZenTest 还是其他东西 我怎样才能克服这个错误 我是 RoR 新手 Invalid gemspec in U

随机推荐