设计通过电子邮件或手机号码注册 [英] Devise sign up either by email or by mobile number

查看:113
本文介绍了设计通过电子邮件或手机号码注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can register a user getting both email address and mobile number in my application. But what I really want is to register user using either email or mobile as a primary authentication key. So that if a user provides email address, it must be saved in email field in database and if user provides mobile number, it must be saved in mobile field in database.

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

And also I would like to overwrite the confirmation method of mobile user and would like to send an message with activation key and inter the key in the application to activate registration. I think its not so hard, but I didn't get where to start. Please suggest me the favourable way to accomplish this task.

推荐答案

啊哈!我做的。我遵循了使用用户名或电子邮件地址进行开发登录,我将用户名更改为手机,并能够使用手机号码登录。但是我发现的问题是我无法使用移动设备注册。因此,基于@Hardik的建议,我在注册表单中使用了一些jQuery来接受手机或电子邮件地址。

Aha! I did it. I followed Devise sign in using username or email address, I changed username to mobile and was able to sign in using the mobile number. But the problem I figured is that I was unable to sign up with mobile. So, based on the suggestion of @Hardik, I used some jQuery in my sign up form to accept either mobile or email address.

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

Since I have allowed Users to set their password themselves after confirming their email address, it became a problem for the mobile number so I checked for the presence of email in request and skipped the confirmation and set a dynamically generated password which I had declared as a instance variable on registrations_controller.rb file which which inherits form Devise Registrations Controller.

用户模型

before_save :check_email


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

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

Hence, I am able to skip confirmation and I provide a default password.

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

Now the application accepts both email address and mobile number. User who register with email address can set their password but for the mobile users, they cannot set their password. So, I used Pilvo SMS api to send their password in their mobile number. For this I used the Pilvo gem and their API and overwrite the Devise Registration Controller.

Gemfile

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

我使用Phonelib验证手机号码。

I used Phonelib for the validation of Mobile Numbers.

  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

它工作,并且我正在收到SMS,但是我不确定这是一种好方法还是安全的方法。如果这不是安全的方法,请建议我采取一种有利的方法。

It worked, and I am getting SMS but I am not sure if it is a good or secure approach or not. If this is not a secure approach, please suggest me a favourable way.

这篇关于设计通过电子邮件或手机号码注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆