1

By default the intridea / omniauth-identity rails gem uses email to login and they are case sensitive. How do I make the login email case insensitive? Adding ":case_sensitive => false" to the model as seen below does not work.

#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

For example, even with the model above if you signed up with "[email protected]" and then tried to login with "[email protected]" the login would produce an OMNIAUTH::ERROR of invalid credentials.

Any suggestions?

2 Answers 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()  )

})
1

You can find solutions in

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

For the sake of clarity...

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

As ujeezy noted above, the omniauth-identity gem on Rubygems does not support this feature, this is still true. Make sure your gemfile is as he stated:

#Gemfile
gem 'omniauth-identity', git: 'https://github.com/intridea/omniauth-identity.git'
4
  • This throws an error on identity creation, fyi. Not a stable solution. May 10, 2014 at 8:34
  • @ericpeters0n More information than "throws an error" should be appreciable. Works fine for me.
    – pierallard
    May 12, 2014 at 14:02
  • NoMethodError at /auth/identity/callback undefined method `downcase' for nil:NilClass (Upon submission of new identity, Fyi @ForgetTheNorm ) May 14, 2014 at 21:04
  • tTo prevent the nil error, you can just use this lambda { |req| { model.auth_key => (req['auth_key'].present? ? req['auth_key'].downcase : req['auth_key']) } }
    – Meier
    Oct 9, 2015 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.