我正在使用 devise-auth-token,并且想要使用电子邮件以外的字段登录。 所以我改用了phone_number,一切正常,但我无法删除电子邮件验证,这是我的代码
用户.rb
class User < ActiveRecord::Base
include DeviseTokenAuth::Concerns::User
devise :database_authenticatable, :confirmable, authentication_keys: [:phone_number]
mount_uploader :avatar, AvatarUploader
validates :first_name, presence: true,format: {with: /[[:word:]]/}
validates :last_name , presence: true,format: {with: /[[:word:]]/}
validates :email , format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }, uniqueness: true
validates :phone_number, presence: true, uniqueness: true
validate :birthdate_in_the_future_invalid
validates :country_code, presence: true
validates :birthdate, presence: true
validates :gender, presence: true
# validates :file, presence: true
validates :phone_number, phone: { possible: true, types: [:mobile], country_specifier: -> phone { phone.country_code.try(:upcase) } }
validate :e164_phone_number
after_create :set_provider_uid
def email_required?
false
end
def email_changed?
false
end
def password_required?
super if confirmed?
end
def password_match(password, password_confirmation)
self.errors[:password] << "can't be blank" if password.blank?
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
password == password_confirmation && !password.blank?
end
private
def birthdate_in_the_future_invalid
begin
if Date.strptime(birthdate, '%d-%m-%Y') > Date.today
errors.add(:birthdate, "in_the_future")
end
rescue
errors.add(:birthdate, "invalid format")
end
end
def e164_phone_number
if phone_number[0] != '+'
errors.add(:phone_number, "invalid format")
end
end
def set_provider_uid
self.update_column(:uid, phone_number)
self.update_column(:provider, "phone_number")
end
end
这是我的初始化程序/devise.rb
Devise.setup do |config|
config.mailer_sender = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb9a97929a999f9e97899a93969a958c9e909acccfbb9c969a9297d5989496" rel="noreferrer noopener nofollow">[email protected]</a>'
require 'devise/orm/active_record'
config.authentication_keys = [:phone_number]
config.case_insensitive_keys = [:phone_number]
config.strip_whitespace_keys = [:phone_number]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 11
config.reconfirmable = false
config.expire_all_remember_me_on_sign_out = true
config.password_length = 6..128
config.email_regexp = /\A[^@\s]+@[^@\s]+\z/
config.reset_password_within = 6.hours
config.sign_out_via = :delete
end
我使用了 email_required 功能?并让它返回 false 但什么也没有发生
我调用 localhost:3000/users ,方法发布 参数:
{
"user": {
"first_name": "ali",
"last_name": "weka",
"gender": "male",
"birthdate": "25-06-2016",
"email": "",
"phone_number": "+201118574649",
"country_code": "EG",
"uid": "+201118574649",
"provider": "phone_number"
}
}
这是回复
{"status":"error","data":{"id":null,"first_name":"ali","last_name":"ali","country_code":"EG","phone_number":"+201119574649","gender":"male","birthdate":"25-06-2016","created_at":null,"updated_at":null,"provider":"email","uid":"+201119574649","avatar":{"url":null},"email":""},"errors":{"email":["can't be blank","is invalid"],"full_messages":["Email can't be blank","Email is invalid"]}}
请问有什么帮助吗?
最佳答案
我也遇到了同样的问题。我想要一些用户有电子邮件,一些用户没有。
解决方案
您应该首先通过迁移从用户表中删除 null: false 限制。如果您想删除默认的“”,请执行此操作。不要删除索引,因此它仍然验证电子邮件是唯一的(以防您有时仍然使用电子邮件)。空值不会有唯一约束的问题。
如果您使用的是 devise validatable,请在您的用户模型中添加此方法:
def email_required?
false
end
完成前一部分后,您应该执行以下操作之一:
选项 1: 深入研究 devise_token_auth gem,至少就我而言,我意识到 this concern被包括在内。
默认情况下包含它,除非您修改 DeviseTokenAuth 初始值设定项
config.default_callbacks = false
但也许你不想要这样。
选项 2: 如果您检查我链接的问题中的代码,您可以看到它会验证电子邮件,以防用户的提供者是“电子邮件”。
我的解决方案是为没有电子邮件的用户将provider字段更改为其他内容。 进行这两项更改中的任何一项后,您的电子邮件将不再得到验证。
对于有时需要电子邮件的人:
- 当然,做什么Devise wiki says
- 除了其他字段外,还包括 case_insensitive_keys、strip_whitespace_keys 中的电子邮件
您可以在用户模型中使用以下方法:
def email_required?
true unless phone_number?
end
关于ruby-on-rails - 无法禁用 devise-auth-token 中的电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559786/