代码之家  ›  专栏  ›  技术社区  ›  user4169418

在authy Device中自定义视图

  •  2
  • user4169418  · 技术社区  · 8 年前

    我正在尝试通过以下方式实现双因素身份验证: authy 2FA of twilio 使用 devise-authy 宝石

    1. 第一页-用户进入的我的登录页面 username password , 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\u authy\u表单中出错 在检查gem代码时,我发现我需要 @authy_id 所以我试过了 <%@authy_id=User.find(session[:user_id]).authy_id%> 看来没有还是没有成功。

    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
    

    我在谷歌上搜索过,但没有找到解决方案

    1 回复  |  直到 8 年前
        1
  •  1
  •   fabOnReact    8 年前

    我正在 错误 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
    

    我相信 @resource nil 所以当它发生的时候 @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 . 因此,也许你应该调试这两段代码,控制器动作负责输入 @资源 form