不区分大小写的omniauth-identity电子邮件登录

时间:2013-04-08 09:22:31

标签: ruby-on-rails omniauth

默认情况下,intridea / omniauth-identity rails gem使用电子邮件登录,它们区分大小写。如何使登录电子邮件不区分大小写?如下所示将“:case_sensitive => false”添加到模型中不起作用。

#models/identity.rb
class Identity < OmniAuth::Identity::Models::ActiveRecord
  attr_accessible :email, :name, :password, :password_confirmation
  validates_presence_of :name
  validates_uniqueness_of :email, :case_sensitive => false
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
end

例如,即使使用上述模型,如果您使用“bob@aol.com”注册,然后尝试使用“BOB@aol.com”登录,则登录将生成无效凭据的OMNIAUTH :: ERROR。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

好的,所以我认为用户ForgetTheNorm有解决方案,但结果是修改了locate_conditions中断注册。解决方案实际上比我原先想象的要简单得多。在使用表单提交之前,使用auth-key进行包装。在使用密钥的任何地方都是如此。例如,登录或注册。

  #login-form
  <%= form_tag('/auth/identity/callback', :method => 'post', :id => 'actual-login-form') do %>
    <div class="field">
      <%= label_tag :auth_key, 'Email' %>
      <br />
      <%= text_field_tag :auth_key, nil, :class => 'user-auth-field user-auth-key' %>
    </div>
    <div class="field">
      <%= label_tag :password %>
      <br />
      <%= password_field_tag :password, nil, :class => 'user-auth-field' %>
    </div>
    <div class="actions">
      <%= submit_tag 'Sign in', :class => 'btn-default', :id => 'modal-sign-in' %>
    </div>
  <% end %>

 #signup-form
  <%= form_tag('/auth/identity/register', :method => 'post', :id => 'actual-signup-form') do %>
    <%= label_tag(nil, 'Choose your username') %>
    <%= text_field_tag 'name', nil, :class => 'signup-form-input', :tabindex => '1' %>

    <%= label_tag(:email, 'Your current email address') %>
    <%= text_field_tag 'email', nil, :class => 'signup-form-input user-auth-key', :tabindex => '2' %>

    <%= label_tag(:password, 'Create a password') %>
    <input type="password" name="password" id="password" class="signup-form-input" tabindex="3" />

    <%= label_tag(:password2, 'Confirm your password') %>
    <input type="password" name="password_confirmation" id="password_confirmation" class="signup-form-input" tabindex="4" />

    <%= submit_tag('Sign Up for Pholder', :id => 'signup-submit', :tabindex => '5') %>
  <% end %>

相关的jQuery / JS:

 $('#actual-login-form, #actual-signup-form').on('submit', function(){

   //lower case auth key (email addresses) for all user validation
 $(this).find('.user-auth-key').val( $(this).find('.user-auth-key').val().toLowerCase()  )

})

答案 1 :(得分:1)

您可以在

中找到解决方案

https://github.com/intridea/omniauth-identity/issues/22

  

为了清楚起见......

#config/initializers/omniauth.rb
provider :identity, locate_conditions: lambda { |req| 
   { model.auth_key => req['auth_key'].downcase }
}
     

正如ujeezy上面提到的,Rubygems上的omniauth-identity gem不会   支持此功能,这仍然是正确的。确保您的gemfile为   他说:

#Gemfile
gem 'omniauth-identity', git: 'https://github.com/intridea/omniauth-identity.git'