Sha256: e5cc737a8be4e6bd10d6b308dd09bff43b1a1370d80bf6946a75669b1f496032

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal
#
# Controller for managing two-factor sessions for <%= plural_class_name %>.
# Automatically generated by the orthodox gem (https://github.com/katanacode/orthodox)
# (c) Copyright 2019 Katana Code Ltd. All Rights Reserved. 
class <%= plural_class_name %>::TfaSessionsController < <%= plural_class_name %>::BaseController
  
  skip_before_action :authenticate_<%= singular_name %>
  
  before_action :authenticate_<%= singular_name %>_without_tfa

  before_action :ensure_<%= singular_name %>_has_active_tfa
  
  before_action :ensure_<%= singular_name %>_not_tfa_authenticated
  
  def new
    @tfa_session = TfaSession.new
  end

  def create
    @tfa_session = TfaSession.new(permitted_params.merge(record: current_<%= singular_name %>))
    if @tfa_session.valid?
      current_<%= singular_name %>.otp_credential.consume_recovery_code!(permitted_params[:recovery_code])
      sign_in(current_<%= singular_name %>, as: :<%= singular_name %>, tfa: true)
      redirect_to <%= singular_name %>_tfa_success_redirect_url
    else
      render :new
    end
  end
  
  private
  
  def permitted_params
    params.require(:tfa_session).permit(:otp, :recovery_code)
  end
  
  def ensure_<%= singular_name %>_has_active_tfa
    return if current_<%= singular_name %>.tfa?
    redirect_to <%= singular_name %>_tfa_success_redirect_url
  end

  def ensure_<%= singular_name %>_not_tfa_authenticated
    if current_<%= singular_name %>_tfa_authenticated?
      redirect_to <%= singular_name %>_tfa_success_redirect_url    
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
orthodox-0.3.3 lib/generators/authentication/templates/controllers/tfa_sessions_controller.rb.erb
orthodox-0.3.2 lib/generators/authentication/templates/controllers/tfa_sessions_controller.rb.erb
orthodox-0.3.1 lib/generators/authentication/templates/controllers/tfa_sessions_controller.rb.erb
orthodox-0.3.0 lib/generators/authentication/templates/controllers/tfa_sessions_controller.rb.erb