【问题标题】:Typus with existing devise model getting 401 unauthorized error具有现有设计模型的 Typus 出现 401 未经授权的错误
【发布时间】:2014-10-17 04:47:06
【问题描述】:

我是 Ruby on Rails 的新手,在使用 devise 配置 typus 时遇到问题。

我有一个 Rails REST 应用程序(使用 rails_api 构建),它使用设计来进行身份验证。目前我正在开发一个新的 Rails 应用程序,它应该是使用 typus 的 REST 应用程序的管理页面。最终,我的目标是让这个管理应用程序访问与之前构建的 REST 应用程序相同的数据库,同时使用 REST 应用程序的现有设计模型。但是,即使用户存在于数据库中,我也会不断收到 401 Unathorized 消息。

确切地说,我在 REST 应用程序中有一个设计模型 Account,我想在我的管理应用程序中使用这个特定的设计模型作为设计模型。我将模型 Account(以及其他模型)从 REST 应用程序复制到新的管理应用程序,然后,我按照 this 将 typus 配置为使用 devise 进行身份验证,但没有生成新的 devise 模型:

rails generate devise:install
rails generate typus

# config/initializers/typus.rb
Typus.setup do |config|
  config.authentication = :devise
  config.user_class_name = "Account"
end

# edit the devise model: Account
require 'typus/orm/active_record/instance_methods'
class Account < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :omniauthable, :confirmable

  include Typus::Orm::ActiveRecord::InstanceMethods

  def locale
    ::I18n.locale
  end

  def role
    Typus.master_role
  end
end

# routes.rb
Rails.application.routes.draw do
  devise_for :accounts
end

# initializers/devise.rb
# make secret_key the same with REST app's secret_key
config.secret_key = 'identic-secret-key-with-rest-app'

当我运行服务器并尝试使用数据库中的现有用户(来自 REST 应用程序的用户)登录时,它总是在服务器控制台中显示 401 Unauthorized。我的配置中有什么遗漏的吗?甚至我的方法是可行的?

如果我为管理应用生成一个新的设计模型,效果会很好。

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4 devise typus


【解决方案1】:

您在注册时在您的帐户模型中添加 :confirmable 它将向您发送确认邮件以确认您的电子邮件,如果您在未确认的情况下登录将引发未经授权的错误。

【讨论】:

  • 谢谢,确实是这个问题,填完confirmed_at栏后问题就解决了。
猜你喜欢
  • 1970-01-01
  • 2016-01-30
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 2022-01-03
  • 2022-11-09
相关资源
最近更新 更多