与常规设计 gem 的 this question 类似,使用 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"
]
}
}
Rails API 是使用 angular2-token 库从 Angular2 调用的,但这显然不是问题(给出来自 Postman 的结果)。
我怎样才能找到这个问题的原因,或者我怎样才能对 gem 进行猴子修补以消除第二个错误?
更新
如果我从模型中删除 :validatable
并进行我自己的验证:
validates_uniqueness_of :email
我得到了相同的结果,这很奇怪。
最佳答案
编辑:此问题已修复,只需升级您的 gem! > v0.1.43.beta1
我找到了 the issue在github上有报道。
解决方法是删除 :validatable(电子邮件的唯一性仍会触发),然后滚动您自己的密码确认验证,如该问题所示(请参阅 stephanebruckert 那里的回复)并在此处转载:
引用: “validatable 验证了电子邮件和密码,这就是为什么我们不应该在模型中做任何这些,否则它会被验证两次。在我的例子中,只有电子邮件被验证了两次,而我没有添加任何额外的在我的模型中进行验证。所以我最终删除了 validatable 并对 password 和 password_confirmation 进行了自己的验证:“
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_confirmation can't be blank error”...这导致了一个兔子洞,发现 this issue .您需要通读所有内容,但基本上,API 应该不强制存在密码确认值(仅在表单本身中需要):)
关于ruby-on-rails - rails 5.1 : devise_token_auth returns 2 errors when only 1 should be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46350596/