设计自定义密码验证导致控制台中的无效用户 [英] Devise custom password validation leads invalid user in console
问题描述
我正在尝试在使用devise进行用户管理的项目中添加自定义密码验证.我可以成功创建用户,或手动更改用户密码.但是,如果退出控制台并再次打开它,则我的有效用户(在最后一步)将变得无效.
I am trying to add custom password validation in a project which I use devise for user management. I am successful to create user, or manually change user password. However, if I exit my console and open it again, my valid user ( at last step ) becomes invalid.
我正在使用devise 4.6.2和rails 5.2.0
I am using devise 4.6.2 and rails 5.2.0
这是我的用户模型
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :password,
format: { with: /\A(?=.*\d)(?=.*[A-Z])(?=.*\W)[^ ]{7,}\z/,
message: 'Password should have more than 7 characters including 1 uppercase letter, 1 number, 1 special character'
}
end
当我在控制台中尝试
u = User.new(email: 'test@test.com', password: 'Abc123!', password_confirmation: 'Abc123!')
u.valid? # TRUE
u.save
然后
u = User.last # return exact above user
u.valid? # FALSE
u.errors.full_messages # Password Password should have more than 7 characters including 1 uppercase letter, 1 number, 1 special character
我在做错什么吗?
推荐答案
User.last没有密码.那就是引发错误的原因.
There's no password for User.last. That reason why error's raised.
非常类似的问题: https://github.com/plataformatec/devise/wiki/操作方法:设置简单密码复杂性要求
噢,你可以在config devise.rb上设置密码的长度.
Anw, u can set length of password on config devise.rb.
config.password_length = 7..128
如果您想在devise.rb上设置密码格式,请尝试使用此gem https://github.com/phatworx/devise_security_extension
Incase u want to set password format on devise.rb, try this gem https://github.com/phatworx/devise_security_extension
这篇关于设计自定义密码验证导致控制台中的无效用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!