OmniAuth 触发验证
我在需要身份验证的应用程序中使用 OmniAuth。
我有 3 种方法供用户进行身份验证:
- 通过 OmniAuth在我的网站上创建帐户
- Facebook 通过 OmniAuth
- Twitter 通过 OmniAuth
对于选项 1,我有以下形式的验证:
validates_presence_of :email, :role
validates_presence_of :password, :if => :password_required
validates_presence_of :password_confirmation, :if => :password_required
validates_length_of :password, :within => 4..40, :if => :password_required
validates_confirmation_of :password, :if => :password_required
validates_length_of :email, :within => 3..100
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_format_of :role, :with => /[A-Za-z]/
问题是,当我允许用户第一次通过 twitter/facebook 登录时创建帐户时,会触发验证并失败。
例如:
ActiveRecord::RecordInvalid - 验证失败:密码不能为空、密码太短(最少为 4 个字符)、密码确认不能为空:
这是有道理的,因为 OmniAuth 创建的帐户不会提交密码,但我'我不确定我应该如何让我的模型意识到这一点并跳过(特定的?)验证。
如果有任何用处,完整的 account.rb 模型在这里: http://pastie.org/private/wzpftprrzfg42uifetfhpa< /a>
非常感谢!
I am using OmniAuth in an application that requires authentication.
I have 3 ways for users to authenticate:
- Create an account on my site
- Facebook via OmniAuth
- Twitter via OmniAuth
For option 1 I have validations in the form of:
validates_presence_of :email, :role
validates_presence_of :password, :if => :password_required
validates_presence_of :password_confirmation, :if => :password_required
validates_length_of :password, :within => 4..40, :if => :password_required
validates_confirmation_of :password, :if => :password_required
validates_length_of :email, :within => 3..100
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_format_of :role, :with => /[A-Za-z]/
The problem is that when I allow a user to login via twitter/facebook for the first time an Account is created, the validations are triggered, and fail.
For example:
ActiveRecord::RecordInvalid - Validation failed: Password can't be blank, Password is too short (minimum is 4 characters), Password confirmation can't be blank:
This makes sense as OmniAuth created accounts will not be submitting a password but i'm not sure exactly how i should make my model aware of this and skip (specific?) validations.
If it's of any use, the full account.rb model is here: http://pastie.org/private/wzpftprrzfg42uifetfhpa
Thanks a lot!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从此处将条件提取到 encrypt_password 方法中,此行似乎不正确:
您还可以复制粘贴堆栈跟踪吗?
try to extract the condition into the encrypt_password method from here, this line seems incorrect:
can you also copy-paste stack trace please?