1

默认情况下,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。

有什么建议么?

4

2 回答 2

2

Ok, so I thought user ForgetTheNorm had the solution, but it turns out modifying the locate_conditions breaks sign up. The solution is actually much simpler than I originally thought. Downcase the auth-key BEFORE it is submitted with the form. This is true of everywhere you use the key. For instance, to login or sign up.

  #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 %>

Relevant 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()  )

})
于 2013-04-11T23:45:42.717 回答
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'
于 2013-04-08T10:25:42.930 回答