Ruby on rails 不区分大小写的omniauth身份电子邮件登录
默认情况下,intridea/omniauth identity rails gem使用电子邮件登录,并且区分大小写。如何使登录电子邮件不区分大小写?将“:case_sensitive=>false”添加到模型(如下所示)不起作用Ruby on rails 不区分大小写的omniauth身份电子邮件登录,ruby-on-rails,omniauth,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
#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
#models/identity.rb
类标识false
验证电子邮件的格式:=>/^[-a-z0-9\+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
结束
例如,即使使用上面的模型,如果您注册了“bob@aol.com“然后尝试使用登录”BOB@aol.com“登录将产生无效凭据的OMNIAUTH::错误
有什么建议吗 您可以在 为了清楚起见
#config/initializers/omniauth.rb
provider :identity, locate_conditions: lambda { |req|
{ model.auth_key => req['auth_key'].downcase }
}
正如ujeezy在上面所指出的,Rubygems上的omniauth身份宝石没有
支持此功能,这仍然是正确的。确保您的GEM文件如下所示
他说:
#Gemfile
gem 'omniauth-identity', git: 'https://github.com/intridea/omniauth-identity.git'
好的,所以我认为用户忘记了这个解决方案,但结果是修改locate_条件会破坏注册。这个解决方案实际上比我原来想象的要简单得多。在将身份验证密钥与表单一起提交之前,请先将其降级。这适用于你使用钥匙的任何地方。例如,登录或注册
#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 %>
这会在身份创建上引发错误,仅供参考。不是一个稳定的解决方案。@ericpeters0n比“抛出错误”更多的信息应该是可感知的。在/auth/identity/callback undefined方法'downcase'中为nil:NilClass(在提交新标识时,Fyi@遗忘形式)运行很好。要防止nil错误,您只需使用这个lambda{req |{model.auth_key=>(req['auth_key']。present??req['auth_key']。downcase:req['auth_key'])}
$('#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() )
})