なるほど!やったよ。私は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が届いていますが、それが良いか安全な方法かどうかはわかりません。これが安全な方法でない場合は、私に有利な方法を提案してください。
'null:false' DB検証を削除しましたか? –