【问题标题】:Customizing the views in authy-devise在 authy-devise 中自定义视图
【发布时间】:2018-04-27 11:42:12
【问题描述】:

我正在尝试使用devise-authy gem 通过authy 2FA of twilio 实现两因素身份验证。

我想为 2FA 自定义包含三个页面的视图。

  1. 第一页 - 我的登录页面,用户输入 usernamepassword, on submit 它将被重定向到一个页面

  2. 第二页 - 在此页面中,他可以选择2FA方法通过电话或短信接收代码,然后重定向

  3. 第三页 - 他终于在这里输入了代码。

我可以配置前两个页面。

我的问题是在第三页,我设置authy form 进行代码验证,我得到错误 undefined method id for nil class

<%= verify_authy_form do %>
  <legend><%= I18n.t('submit_token_title', {:scope => 'devise'}) %></legend>
  <%= label_tag :token %>
      <%= text_field_tag :token, "", :autocomplete => :off, :id => 'authy-token' %>
  <label>
  <%= check_box_tag :remember_device %>
      <span><%= I18n.t('remember_device', {:scope => 'devise'}) %></span>
  </label>

  <!-- Help tooltip -->
  <!-- You need to configure a help message. -->
  <!-- See documentation: https://github.com/authy/authy-form-helpers#help-tooltip -->
  <!-- <%= link_to '?', '#', :id => 'authy-help' %> -->

  <%= authy_request_sms_link %>
  <%= authy_request_phone_call_link %>
  <%= submit_tag I18n.t('submit_token', {:scope => 'devise'}), :class => 'btn' %>
<% end %>

verify_authy_form 这一行出现错误 在检查 gem 的代码时,我发现我需要 @authy_id 所以我尝试了 &lt;%@authy_id=User.find(session[:user_id]).authy_id%&gt; 鉴于没有仍然没有成功。

这是我的Users::AuthyCustomController,我已经覆盖了 gem 中所述的一些方法

class Users::AuthyCustomController  < Devise::DeviseAuthyController

protected
def after_authy_enabled_path_for(resource)
  my_own_path
end

def after_authy_verified_path_for(resource)
  my_own_path
end

def after_authy_disabled_path_for(resource)
  my_own_path
end

def invalid_resource_path
  my_own_path
end

def authentication_sms    
end

def authentication_phone
  @authy_id=User.find(session[:user_id]).authy_id
  # redirect_to user_verify_authy_path
  # redirect_to user_verify_authy_path and return
end
end

我用谷歌搜索了,但我无法找到解决方案

【问题讨论】:

标签: ruby-on-rails devise twilio two-factor-authentication authy


【解决方案1】:

我收到错误 undefined method id for nil class

这是form helper

def verify_authy_form(opts = {}, &block)
  opts = default_opts.merge(:id => 'devise_authy').merge(opts)
  form_tag([resource_name, :verify_authy], opts) do
    buffer = hidden_field_tag(:"#{resource_name}_id", @resource.id)
    buffer << capture(&block)
  end
end

我相信@resourcenil,所以当它发生时@resource.id 会触发错误

我相信这个表单是从这个controller action管理的

# verify 2fa
def POST_verify_authy
  token = Authy::API.verify({
    :id => @resource.authy_id,
    :token => params[:token],
    :force => true
  })

  if token.ok?
    @resource.update_attribute(:last_sign_in_with_authy, DateTime.now)

    session["#{resource_name}_authy_token_checked"] = true

    remember_device if params[:remember_device].to_i == 1
    if session.delete("#{resource_name}_remember_me") == true && @resource.respond_to?(:remember_me=)
      @resource.remember_me = true
    end
    sign_in(resource_name, @resource)

    set_flash_message(:notice, :signed_in) if is_navigational_format?
    respond_with resource, :location => after_sign_in_path_for(@resource)
  else
    handle_invalid_token :verify_authy, :invalid_token
  end
end

您可以通过检查并包含来自rake routes 的相关输出来证明这一点。所以也许你应该调试那两段代码,控制器动作负责将@resource 提供给form

【讨论】:

  • @Fabrizio 谢谢你解决了这个问题非常感谢:)
  • @Fabrizio 你用过这个 gem devise-authy 在用户成功验证了 otp 但提交 otp 后,#POST_verify_authy 在用户登录和默认重定向到主页。你能给我一些建议我该怎么做吗
  • @yogeshmanjhi github.com/authy/… 您需要覆盖这些操作并将您的 your_controller_path 包含在该 ovverriden 操作中。您可以在终端中找到your_controller_pathrake routes。更多关于path helpersguides.rubyonrails.org/routing.html#path-and-url-helpers和控制器guides.rubyonrails.org/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多