我有一个功能齐全的身份验证系统,其用户表有超过50列。它很简单,但它使用salt进行散列加密,使用电子邮件而不是用户名,并且还有两种不同类型的用户和管理员。
我希望将Devise身份验证合并到我的应用程序中,以增强电子邮件验证,忘记密码,记住我的令牌等额外部分...我只是想看看是否有人有任何建议或问题他们已经将Devise合并到现有用户结构中时遇到的问题。我的用户模型中的基本字段是:
t.string :first_name, :null => false
t.string :last_name, :null => false
t.string :email, :null => false
t.string :hashed_password
t.string :salt
t.boolean :is_userA, :default => false
t.boolean :is_userB, :default => false
t.boolean :is_admin, :default => false
t.boolean :active, :default => true
t.timestamps
为了便于参考,这里是迁移中的Devise字段:
t.database_authenticatable :null => false
t.confirmable
t.recoverable
t.rememberable
t.trackable
add_index "users", ["confirmation_token"], :name => "index_users_on_confirmation_token", :unique => true
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
最终会转变为架构中的这些实际字段:
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "reset_password_token"
t.string "remember_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
你们推荐什么?我只是从迁移中删除电子邮件,hashed_password和salt并放入5 Devise迁移字段,一切都会好或我还需要做其他事情吗?
编辑:
我自己开始尝试这个并且已经遇到了一些问题。我将上面显示的设计迁移字段添加到我现有的用户模型中,现在当我运行我的种子文件时,它给了我这个Postgresql错误:
ERROR: duplicate key value violates unique constraint "index_users_on_email"
我的种子档案:
initial_usersA = User.create!(
[
{
:first_name => "John",
:last_name => "Doe",
:email => "johndoe@gmail.com",
:is_userA => true,
:is_userB => false,
:is_admin => true,
:password => "password",
:password_confirmation => "password"
},
{
:first_name => "Jane",
:last_name => "Smith",
:email => "janesmith@gmail.com",
:is_userA => true,
:is_userB => false,
:is_admin => true,
:password => "password",
:password_confirmation => "password"
}
用户模型:
devise :registerable, :authenticatable, :recoverable,
:rememberable, :trackable, :validatable
attr_accessor :password_confirmation, :email, :password
堆栈跟踪显示,由于某种原因,电子邮件显然没有被其他变量输入...虽然种子文件中的其他内容都显示在实际查询中,但是电子邮件是“某些即使它明确定义了.auth
答案 0 :(得分:2)
我记得当我们做类似事情时遇到的两个主要考虑因素是:
数据库迁移 - 我们编写了单独的t.database_authenticatable
语句,而不是使用add_column and rename_column
帮助程序,因此我们没有遇到您看到的任何重复的列或索引错误,因此我们可以重复使用我们的盐和在Devise中对密码进行哈希处理,而无需修改gem的工作方式。
第二个也是更大的考虑因素是我们使用的散列算法与Devise提供的算法不同,因此我们必须将自己的加密器类编写为Devise::Encryptors::Base
的子类,并实现使用我们自己的逻辑消化功能。最后,我们将Devise配置为使用此加密器,在适当的配置/初始化文件中使用config.encryptor = :our_own_algorithm
我希望这足以让你开始。
答案 1 :(得分:0)
我摆脱了:default =>在您的电子邮件架构中。“默认情况下,devise会对电子邮件设置唯一约束,因此您不希望默认为空字符串
答案 2 :(得分:0)
我去年将authLogic(我认为)的应用程序切换到了Devise,我的课程是: - 用户表可以保留 - 将列重命名为Devise标准,我确定这不是必需的,但与其他身份验证方法不同,我没有找到lib映射文件,我可以在其中添加不同的db字段名称,就像我使用其他身份验证方法一样
实际上是关于它的。我真的很惊讶我需要做的很少。我认为我的哈希实际上仍然有效,或者我按照指示中的指示改为常规。
我使用带有Devise的'admin'标志路由,所以从当前使用的任何东西进行sql转换都可以做到。
我也会摆脱默认的“”位,我有一个生产应用程序(不知道它使用什么身份验证,我认为是一些基本的东西),看起来像: t.string“EMAIL”,:limit => 64,:null =>假 t.string“PASSWORD”,:limit => 64,:null =>假