通过电子邮件或手机号码设计注册

我可以注册用户在我的应用程序中获取电子邮件地址和手机号码。 但我真正想要的是使用电子邮件或移动设备注册用户作为主要身份validation密钥。 因此,如果用户提供电子邮件地址,则必须将其保存在数据库的电子邮件字段中,如果用户提供移动电话号码,则必须将其保存在数据库的移动字段中。

此外,我想覆盖移动用户的确认方法,并希望发送带有激活密钥的消息,并在应用程序中输入密钥以激活注册。 我认为它不是那么难,但我没有从哪里开始。 请建议我完成这项任务的有利方法。

是的,您可以通过轻微设置轻松完成。

像这样

修改application_controller.rb

class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters added_attrs = [:mobile_no, :email, :password, :password_confirmation, :remember_me] devise_parameter_sanitizer.permit :sign_up, keys: added_attrs devise_parameter_sanitizer.permit :account_update, keys: added_attrs end end 
 Create a login virtual attribute in the User model 
 Add login as an attr_accessor: # Virtual attribute for authenticating by either username or email # This is in addition to a real persisted field like 'username' attr_accessor :login or, if you will use this variable somewhere else in the code: def login=(login) @login = login end def login @login || self.mobile_no || self.email end 

修改config / initializers / devise.rb以具有:

 config.authentication_keys = [ :login ] 

您可以参考此链接以获取更多参考。

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

啊哈! 我做的。 我使用用户名或电子邮件地址跟踪Devise登录 ,我将用户名更改为移动设备,并且能够使用手机号码登录。 但我想到的问题是我无法注册移动设备。 因此,根据@Hardik的建议,我在注册表单中使用了一些jQuery来接受移动或电子邮件地址。

由于我已经允许用户在确认他们的电子邮件地址后自己设置密码,因此它成为手机号码的问题所以我在请求中检查了电子邮件的存在并跳过确认并设置了我声明为动态生成的密码。 registrations_controller.rb文件中的实例变量,它inheritance了Devise Registrations Controller的forms。

用户模型

 before_save :check_email def check_email if !self.email.present? skip_confirmation! end end 

因此,我可以跳过确认,并提供默认密码。

现在该应用程序接受电子邮件地址和手机号码。 注册电子邮件地址的用户可以设置密码,但对于移动用户,他们无法设置密码。 所以,我使用Pilvo SMS api在他们的手机号码中发送密码。 为此我使用了Pilvo gem及其API并覆盖了Devise Registration Controller。

的Gemfile

 gem 'phonelib' gem 'plivo', '~> 0.3.19' 

我使用Phonelibvalidation手机号码。

  require 'rubygems' require 'plivo' include Plivo class RegistrationsController < Devise::RegistrationsController prepend_before_action :require_no_authentication, only: [:new, :create, :cancel] prepend_before_action :authenticate_scope!, only: [:edit, :update, :destroy] prepend_before_action :set_minimum_password_length, only: [:new, :edit] AUTH_ID = "PLIVO AUTH ID" AUTH_TOKEN = "PLIVO AUTH TOKEN" # GET /resource/sign_up def new super end # POST /resource def create if !sign_up_params[:email].present? @password = Devise.friendly_token.first(8) #begin register build_resource resource.password = @password resource.mobile = sign_up_params[:mobile] if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up_mobile if is_navigational_format? #redirect_to root_path respond_with resource, :location => after_sign_up_path_for(resource) p = RestAPI.new(AUTH_ID, AUTH_TOKEN) # Send SMS params = { 'src' => '+9779855065526', # Sender's phone number with country code 'dst' => '+'+ sign_up_params[:mobile].to_s, # Receiver's phone Number with country code 'text' => t('sms.register', :password => @password.to_s).html_safe, # Your SMS Text Message - English 'method' => 'POST' # The method used to call the url } response = p.send_message(params) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location => after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource respond_with resource end else super end end protected def after_sign_up_path_for(resource) root_path # Or :prefix_to_your_route end def after_update_path_for(resource) if admin? control_index_path elsif merchant? merchants_path elsif customer? customers_path end end end 

它工作,我收到短信,但我不确定它是否是一个好的或安全的方法。 如果这不是一个安全的方法,请建议我一个有利的方式。