2

I am trying to implement two factor authentication via authy 2FA of twilio using devise-authy gem.

I want to customize the views with three pages for 2FA.

  1. first page - My login page where user enters username and password, on submit it will be redirected to a page

  2. Second page - In this page he can select the 2FA method to receive the code via phone or sms after which he is redirected

  3. Third page - Here he finally enters the code.

I am able to configure the first two pages.

My PROBLEM is that in the third page, where i am setting up the authy form for code verification, I am getting error 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 %>

I am getting error in this line verify_authy_form On inspecting the code of gem i found that i need @authy_id so i tried <%@authy_id=User.find(session[:user_id]).authy_id%> in view no still no success.

This is my Users::AuthyCustomController, where i have overridden some of the methods as stated in 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

I have googled, but I was not able to find a solution

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • https://www.twilio.com/docs/api/authy#authy-api `verify_authy_form` is a helper so I wonder where I can read that code, I can not find the github page of this specific project – Fabrizio Bertoglio Nov 14 '17 at 14:09
  • check it here https://github.com/authy/authy-devise/blob/master/app/controllers/devise/devise_authy_controller.rb –  Nov 14 '17 at 14:27

1 Answers1

1

I am getting error undefined method id for nil class

This is the 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

I belive that @resource is nil so when it does @resource.id triggers the error

I believe this form is managed from this 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

and you can prove that by checking and including the relevant output from rake routes. So maybe you should debug that two pieces of code, the controller action is responsible to feeding @resource to the form

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • @ Fabrizio thanks that solved the problem thanks a lot :) –  Nov 15 '17 at 06:18
  • @ Fabrizio have you used this gem devise-authy i am trying to redirect to specific page after user has successfully verified the otp but after the otp is submitted the #POST_verify_authy gets called where the user is sign_in and by default redirected to home page.Can you please give some suggestion how can i do this –  Nov 15 '17 at 10:46
  • @yogeshmanjhi https://github.com/authy/authy-devise#custom-redirect-paths-eg-using-modules you need to override those actions and include in that ovverriden action your `your_controller_path`. You can find `your_controller_path` with `rake routes` in the terminal. More about `path helpers` http://guides.rubyonrails.org/routing.html#path-and-url-helpers and controllers http://guides.rubyonrails.org/action_controller_overview.html#methods-and-actions – Fabrizio Bertoglio Nov 15 '17 at 12:46