2016-07-06 189 views
3

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

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

回答

1

啊哈!我做的。我跟着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 

它的工作,我得到短信,但我不知道如果这是一个好或安全的方法或不。如果这不是一个安全的方法,请给我一个有利的方法。

+0

你是否删除了'null:false'数据库验证? –

4

是的,你可以轻松地做小设置。

这样

修改您的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 

修改配置/初始化ializers/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

+0

我使用电子邮件或用户名登录。但我想用电子邮件或手机在相同的文本字段中注册输入。 –

+0

你可以使用正则表达式检查文本字段的值,并通过jquery检查其电子邮件或手机号码,并根据 –

+0

设置字段以及这是一个想法,以及如何覆盖移动确认。像你一样,如果电子邮件字段为空,移动字段具有手机号码,那么如何在确认控制器 –