gpt4 book ai didi

ruby-on-rails - 将设计身份验证合并到现有的用户结构中?

转载 作者:行者123 更新时间:2023-12-04 12:50:00 25 4
gpt4 key购买 nike

我有一个功能齐全的身份验证系统,其中的用户表有 50 多列。这很简单,但它使用盐进行哈希加密,使用电子邮件而不是用户名,并且还有两种不同的用户和管理员。

我希望将设计身份验证合并到我的应用程序中以加强额外的部分,如电子邮件验证、忘记密码、记住我的 token 等......我只是想看看是否有人在合并时遇到任何建议或问题设计成一个已经存在的用户结构。我的用户模型中的基本字段是:

  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

作为引用,这里是迁移中的设计字段:
  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_pa​​ssword 和 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

最佳答案

我记得我们在做类似事情时面临的两个主要考虑是:

数据库迁移 - 而不是使用 t.database_authenticatable helper ,我们写了个人add_column and rename_column语句,这样我们就不会遇到您看到的任何重复的列或索引错误,这样我们就可以在 Devise 中重用我们的 salt 和散列密码,而无需修改 gem 的工作方式。

第二个也是更大的考虑是,我们使用的哈希算法与 Devise 提供的算法不同,因此我们必须编写自己的加密器类作为 Devise::Encryptors::Base 的子类。 ,并使用我们自己的逻辑实现摘要功能。最后,我们通过在适当的配置/初始化文件中使用 config.encryptor = :our_own_algorithm 指定它来配置 Devise 以使用该加密器。

我希望这足以让您入门。

关于ruby-on-rails - 将设计身份验证合并到现有的用户结构中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2846463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com