首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义authy-devise中的视图

自定义authy-devise中的视图
EN

Stack Overflow用户
提问于 2017-11-14 13:36:05
回答 1查看 588关注 0票数 2

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

我想为2FA定制三页的视图。

  1. 第一页-我的登录页面,用户输入usernamepasswordon submit它将被重定向到一个页面。
  2. 第二页-在此页面中,他可以选择2FA方法,通过电话或sms接收代码,然后重定向。
  3. 第三页-他终于输入了密码。

我能够配置前两页。

我的问题是,在第三页中,在设置用于代码验证的authy form时,我得到了error undefined method id for nil class

代码语言:javascript
复制
<%= 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 %>

在检查gem的代码时,这一行verify_authy_form出错了,我发现我需要@authy_id,所以我尝试了<%@authy_id=User.find(session[:user_id]).authy_id%>,但是没有成功。

这是我的Users::AuthyCustomController,在这里,我重写了gem中所述的一些方法

代码语言:javascript
复制
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

我搜索过,但我找不到解决办法

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-14 14:53:48

我得到了错误 undefined method id for nil class

这是表单助手

代码语言:javascript
复制
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会触发错误

我相信这个表单是从这个控制器动作管理的。

代码语言:javascript
复制
# 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47287240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档