Rails: Twilio::REST::RestError



我已将twilio gem添加到我的应用程序中以验证电话号码。这是我的设置:

控制器:

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :set_client, only: [:create, :verify]
before_action :current_user, only: [:verify]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
channel = user_params['channel']
@user = User.new(user_params.except('channel', 'displayed_phone_number'))
respond_to do |format|
if @user.save
start_verification(@user.phone_number, channel)
session[:user_id] = @user.id
format.html { redirect_to verify_url, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
def verify
if request.post?
is_verified = check_verification(@current_user.phone_number, params['verification_code'])
if is_verified
@current_user.verified = true
@current_user.save
respond_to do |format|
format.html { redirect_to main_index_url, notice: 'User was successfully verified.' }
end
else
respond_to do |format|
format.html { redirect_to verify_url, notice: 'The code was invalid.' }
end
end
else
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
def set_client
@client = Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:username, :password, :password_confirmation, :phone_number)
end
def start_verification(to, channel='sms')
channel = 'sms' unless ['sms', 'voice'].include? channel
verification = @client.verify.services(ENV['VERIFICATION_SID'])
.verifications
.create(:to => to, :channel => channel)
return verification.sid
end
def check_verification(phone, code)
verification_check = @client.verify.services(ENV['VERIFICATION_SID'])
.verification_checks
.create(:to => phone, :code => code)
return verification_check.status == 'approved'
end
end

形式:

<%= form_with(model: user, local: true) do |form| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :username %>
<%= form.text_field :username %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.password_field :password %>
</div>
<div class="field">
<%= form.label :password_confirmation %>
<%= form.password_field :password_confirmation %>
</div>
<div class="field">
<%= form.label :displayed_phone_number %>
<%= form.text_field :displayed_phone_number, type: 'tel' %>
</div>
<div class="field">
<label for="channel">Verification Method</label>
<label><input type="radio" name="channel" value="sms" checked>SMS</label>
<label><input type="radio" name="channel" value="voice">Call</label>
</div>
<div class="actions">
<%= form.submit 'Sign Up' %>
</div>
<% end %>

<script>
var input = document.querySelector("#user_displayed_phone_number");
window.intlTelInput(input, {
hiddenInput: "phone_number",
preferredCountries: ["us", "gb", "co", "de"]
});
</script>

型:

class User < ApplicationRecord
has_secure_password
validates :username, presence: true, uniqueness: true
validates :phone_number, presence: true, uniqueness: true
end

Veirfy 形式:

<% title 'Verify' %>
<header><h1>Verify</h1></header>
<p id="notice"><%= notice %></p>
<%= form_tag(verify_url) do %>
<div class="field">
<%= label_tag :verification_code %>
<%= text_field_tag :verification_code %>
</div>
<div class="actions">
<%= submit_tag 'Verify' %>
</div>
<% end %>

路线:

Rails.application.routes.draw do
get 'main/index'
resources :users, only: [:new, :create, :show, :index, :destroy]
resources :sessions, only: [:new, :create, :destroy]
get 'verify', to: 'users#verify', as: 'verify'
post 'verify', to: 'users#verify'
get 'register', to: 'users#new', as: 'register'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
root 'main#index'
end

当我对此进行测试时,我收到带有验证码的电话或短信。我在验证表单中输入它,一切正常,电话号码得到验证。但是当我输入错误的代码或根本不输入代码时......而不是收到通知消息The code was invalid.我收到此错误:

Twilio::REST::RestError in UsersController#verify
[HTTP 400] 60200 : Unable to create record Invalid parameter: Code https://www.twilio.com/docs/errors/60200

这是来自用户控制器和此方法:

def check_verification(phone, code)
verification_check = @client.verify.services(ENV['VERIFICATION_SID']).verification_checks.create(:to => phone, :code => code)
return verification_check.status == 'approved'
end

错误出在这一行:

verification_check = @client.verify.services(ENV['VERIFICATION_SID']).verification_checks.create(:to => phone, :code => code)

知道为什么会这样吗?

Twilio开发者布道者在这里。

该错误消息指出代码参数无效。我假设在不发送代码的情况下你会得到这一点。因此,如果代码为空,我可能会在您的check_verification方法中构建早期返回(无论如何它都不会是正确的(。

def check_verification(phone, code)
return false if code.blank?
verification_check = @client.verify.services(ENV['VERIFICATION_SID'])
.verification_checks
.create(:to => phone, :code => code)
return verification_check.status == 'approved'
end

至于提交错误的代码,您是否会收到相同的错误和相同的消息?

最新更新