2016-07-06 32 views
3

私のアプリケーションでメールアドレスと携帯電話番号の両方を取得するユーザーを登録できます。しかし、私が本当に望んでいるのは、電子メールかモバイルのいずれかを使ってユーザーをプライマリ認証キーとして登録することです。したがって、ユーザが電子メールアドレスを提供する場合、データベースの電子メールフィールドに保存しなければならず、ユーザが携帯番号を提供する場合、それはデータベースのモバイルフィールドに保存されなければならない。電子メールまたは携帯電話番号で登録する

また、モバイルユーザーの確認方法を上書きしたいと思います。登録を有効にするために、アクティベーションキーとそのキーの間にメッセージを送信したいと思います。私はそれほど難しくないと思っていますが、どこから始めるのか分かりませんでした。この仕事を達成するための好都合な方法を教えてください。

答えて

1

なるほど!やったよ。私はDevise sign in using username or email addressに続き、ユーザー名をモバイルに変更し、モバイル番号を使ってサインインすることができました。しかし、私が考えた問題は、モバイルで申し込むことができなかったことです。だから、@ Hardikの提案に基づいて、モバイルまたは電子メールアドレスのどちらかを受け入れるためにサインアップフォームにjQueryを使用しました。私は、ユーザーが自分のメールアドレスを確認した後にパスワードを自分で設定することができましたので、それは携帯電話の番号のために問題となった

ので、私は、要求における電子メールの有無をチェックし、確認をスキップして、動的に生成されたパスワードを設定しているIは、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の宝石とその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 

SMSが届いていますが、それが良いか安全な方法かどうかはわかりません。これが安全な方法でない場合は、私に有利な方法を提案してください。

+0

'null:false' DB検証を削除しましたか? –

4

はい、マイナーな設定で簡単に行うことができます。

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 

変更設定/ initをごapplication_controller.rbこの

よう

を変更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

に従ってフィールドを設定したりすることができます。これはアイデアとモバイルの確認を上書きする方法です。あなたのように電子メールのフィールドが空白でモバイルフィールドに携帯番号がある場合、どのようにsmsが確認コントローラ –

関連する問題