设计:允许用户注册为“UsErNaMe”但是用“用户名” [英] Devise: Allow users to register as "UsErNaMe" but login with "username"
问题描述
这是一个相当明显的和必要的功能,并且大量的人似乎已经问了,但是我保持绊倒的解决方案似乎与Devise自己的文档断开连接。
例如,考虑这个博文: http://anti-pattern.com/2011/5/16/case -inensitive-keys-with-devise
[...]你可能遇到了一些用户的问题喜欢在他们的登录(电子邮件和/或用户名)中键入
某些字母大写,
,但希望在尝试登录时不区分大小写。不是
不合理的请求[ ...]
很酷!这就是我想要的。
他的解决方案:
#config / initializer / devise.rb
Devise.setup do | config |
config.case_insensitive_keys = [:email,,username]
end
我一直在找的解决方案。但是以下是该配置选项的文档:
#配置哪些验证密钥不区分大小写。
#这些键将在创建或修改用户时使用
#来进行身份验证或查找用户。默认是:电子邮件。
config.case_insensitive_keys = [:username,:email]
特别是:这些键将在创建/修改用户后下载。换句话说,数据库中的用户名正在缩减。
要验证:
code> User.create用户名:UsErNaMe,密码:secret,电子邮件:email@com.com
#=> < User username =username...>
我错过了一些令人痛苦的事情吗?
从 devise wiki :您需要在模型中覆盖devise的 find_first_by_auth_conditions
方法。
ActiveRecord示例:
def self.find_first_by_auth_conditions(warden_conditions)
条件= warden_conditions.dup
如果login = conditions.delete(:login)
其中(条件).where([lower(username)=:value OR lower(email)=:value,{:value => login .downcase}])。first
else
其中(条件).first
end
end
如果您不需要电子邮件,也可以删除或更低(电子邮件)=:value
部分。
这样你不需要在 case_inse中列出
,它不会在数据库中下载。 username
nsitive_keys
In the same way most websites work, I was to store "UsErNaMe" in the database but let users login with "username".
This is a fairly obvious and necessary feature, and plenty of people seem to have asked it, but the solution I keep stumbling upon seems disconnected from Devise's own documentation.
For instance, consider this blog post: http://anti-pattern.com/2011/5/16/case-insensitive-keys-with-devise
[...]you’ve probably run into the problem that some users like to type certain letters in their logins (email and/or username) in uppercase, but expect it to be case-insensitive when they try to sign in. A not unreasonable request[...]
Cool! That's what I want.
His solution:
# config/initializers/devise.rb
Devise.setup do |config|
config.case_insensitive_keys = [:email, :username]
end
That's the solution I keep finding. But here's the documentation for that config option:
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
# to authenticate or find a user. Default is :email.
config.case_insensitive_keys = [ :username, :email ]
In particular: "These keys will be downcased upon creating/modifying a user." In other words, the username is being downcased in the database.
To verify:
User.create username: "UsErNaMe", password: "secret", email: "email@com.com"
#=> <User username="username"...>
Am I missing something painfully obvious?
From devise wiki: you need to overwrite devise's find_first_by_auth_conditions
method in your model.
ActiveRecord example:
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
You can remove OR lower(email) = :value
part if you don't need auth by email too.
That way you don't need to list username
in case_insensitive_keys
and it wouldn't be downcased in the database.
这篇关于设计:允许用户注册为“UsErNaMe”但是用“用户名”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!