OmniAuth 触发验证

发布于 2024-11-24 18:53:03 字数 1332 浏览 0 评论 0原文

我在需要身份验证的应用程序中使用 OmniAuth。

我有 3 种方法供用户进行身份验证:

  1. 通过 OmniAuth在我的网站上创建帐户
  2. Facebook 通过 OmniAuth
  3. 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:

  1. Create an account on my site
  2. Facebook via OmniAuth
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-12-01 18:53:03

尝试从此处将条件提取到 encrypt_password 方法中,此行似乎不正确:

  # Callbacks
  before_save :encrypt_password, :if => :password_required

您还可以复制粘贴堆栈跟踪吗?

try to extract the condition into the encrypt_password method from here, this line seems incorrect:

  # Callbacks
  before_save :encrypt_password, :if => :password_required

can you also copy-paste stack trace please?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文