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

时间:2016-07-06 10:31:30

标签: ruby-on-rails devise

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

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

2 个答案:

答案 0 :(得分:4)

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

像这样

  

修改application_controller.rb

syncmeth.pas(57) Warnung: Symbol 'AllocateHWnd' wird abgelehnt

syncmeth.pas(62) Warnung: Symbol 'DeallocateHWnd' wird abgelehnt
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
  

修改config / initializers / devise.rb以获得:

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

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

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

答案 1 :(得分:2)

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

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

用户模型

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'

我使用Phonelib验证手机号码。

  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

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

相关问题