我可以在我的應用程序中註冊一位同時獲得電子郵件地址和手機號碼的用戶。但我真正想要的是使用電子郵件或手機註冊用戶作爲主要身份驗證密鑰。因此,如果用戶提供電子郵件地址,則必須將其保存在數據庫的電子郵件字段中,如果用戶提供手機號碼,則必須將其保存在數據庫的移動字段中。通過電子郵件或手機號碼設計註冊
另外我想覆蓋移動用戶的確認方法,想用激活碼發送消息並在應用程序中激活密鑰以激活註冊。我認爲它不那麼難,但我沒有從哪裏開始。請建議我完成此任務的有利方式。
我可以在我的應用程序中註冊一位同時獲得電子郵件地址和手機號碼的用戶。但我真正想要的是使用電子郵件或手機註冊用戶作爲主要身份驗證密鑰。因此,如果用戶提供電子郵件地址,則必須將其保存在數據庫的電子郵件字段中,如果用戶提供手機號碼,則必須將其保存在數據庫的移動字段中。通過電子郵件或手機號碼設計註冊
另外我想覆蓋移動用戶的確認方法,想用激活碼發送消息並在應用程序中激活密鑰以激活註冊。我認爲它不那麼難,但我沒有從哪裏開始。請建議我完成此任務的有利方式。
啊哈!我做的。我跟着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
它的工作,我得到短信,但我不知道如果這是一個好或安全的方法或不。如果這不是一個安全的方法,請給我一個有利的方法。
是的,你可以輕鬆地做小設置。
這樣
修改您的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 ]
您可以參考更多的參考此鏈接。
我使用電子郵件或用戶名登錄。但我想用電子郵件或手機在相同的文本字段中註冊輸入。 –
你可以使用正則表達式檢查文本字段的值,並通過jquery檢查其電子郵件或手機號碼,並根據 –
設置字段以及這是一個想法,以及如何覆蓋移動確認。像你一樣,如果電子郵件字段爲空,移動字段具有手機號碼,那麼如何在確認控制器 –
你是否刪除了'null:false'數據庫驗證? –