与常规设计的GEM相似,使用devise_token_auth Gem显示了相同的结果 - 在JSON响应中出现了两次验证错误!?
来自日志:
Processing by DeviseTokenAuth::RegistrationsController#create as JSON
Parameters: {"name"=>"Mickey Mouse", "email"=>"mickeymouse@gmail.com", "password"=>"[FILTERED]", "confirmPassword"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:4200/register", "registration"=>{"name"=>"Mickey Mouse", "email"=>"mickeymouse@gmail.com", "password"=>"[FILTERED]", "confirmPassword"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:4200/register"}}
Unpermitted parameters: :confirmPassword, :confirm_success_url, :registration
Unpermitted parameters: :confirmPassword, :confirm_success_url, :registration
(0.2ms) BEGIN
User Exists (0.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "mickeymouse@gmail.com"], ["LIMIT", 1]]
(0.8ms) SELECT COUNT(*) FROM "users" WHERE "users"."provider" = $1 AND "users"."email" = $2 [["provider", "email"], ["email", "mickeymouse@gmail.com"]]
(0.3ms) ROLLBACK
Completed 422 Unprocessable Entity in 247ms (Views: 0.7ms | ActiveRecord: 6.9ms)
请注意,Unpermitted_parameters系列显示了两次 - 这似乎表明了一些奇怪的东西(这些行不会通过Postman显示)。
我的用户模型在标准指南中没有任何额外的内容,因此我绝对没有2个唯一性或在我的模型上验证,并且检查了GEM的源代码,它似乎也没有。
这是模型:
class User < ActiveRecord::Base
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
end
如果我从Postman中调用此终点,我得到相同的结果,这是返回的JSON:
{
"status": "error",
"data": {
"id": null,
"account_id": null,
"provider": "email",
"uid": "",
"name": null,
"nickname": null,
"image": null,
"email": "mickeymouse@gmail.com",
"created_at": null,
"updated_at": null
},
"errors": {
"email": [
"has already been taken",
"has already been taken"
],
"full_messages": [
"Email has already been taken",
"Email has already been taken"
]
}
}
使用Angular2-Token库从Angular2调用Rails API,但这显然不是问题(给出邮递员的结果)。
如何找到此原因,或者如何猴子绘制宝石以删除第二个错误?
update
如果我从模型中删除:validatable
并进行自己的验证:
validates_uniqueness_of :email
我得到了相同的结果,这很奇怪。
edit:这个问题已经 fileve ,只需升级您的宝石即可!> v0.1.43.beta1
我发现该问题已在GitHub上报告。
解决方法是删除:有效的(电子邮件唯一性仍然会发射),然后按照该问题所示(请参阅Stephanebruckert的响应)对密码确认进行验证,并在此处复制:
报价:"有效验证了电子邮件和密码,这就是为什么我们不应该在模型中进行任何操作,否则将两次验证。在我的情况下,只有两次验证了两次,而我没有添加任何额外的额外在我的模型中验证。因此,我最终删除了有效的效果,并在密码和密码上进行了自己的验证:"
validates_presence_of :password, on: :create
validates :password,
length: { minimum: 8 },
allow_blank: true
validate :password_complexity
validates_confirmation_of :password
validates_presence_of :password_confirmation, if: lambda {| u| u.password.present? }
private
def password_complexity
return unless password
if password.include?(username) || !password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)./)
errors.add(:password, "Must include at least one lowercase letter, one uppercase letter and one digit")
end
end
尽管这确实解决了重复的电子邮件错误,但我现在得到一个'password_confircation不能是空白错误'...这会导致兔子孔发现此问题。您将需要全部读取所有内容,但基本上,API应不是执行密码确认值的存在(仅在表单本身中需要):)