Home > Mobile >  Rails enable before_action only on development env
Rails enable before_action only on development env

Time:11-14

In my Rails 7 app I've got custom two_factor_authentication. Now I want to enable this 2fa only in development and test environment.

class ApplicationController < ActionController::Base
  before_action :check_two_factor_authentication, except: :check_authentication

  private

  def check_two_factor_authentication
    return true unless session[:challenge_id]

    redirect_to two_factor_path
  end

I've no idea what is check_authentication because inside entire app there is no such method - it comes from Devise gem I guess.

How to set that before action only to development environment?

CodePudding user response:

have you tried

if %w(development test).include?(Rails.env)
  before_action :check_two_factor_authentication, except: :check_authentication 
end

?

CodePudding user response:

You can use Rails.env.development? (or Rails.env.production?) to test which environment you are in.

if Rails.env.development?
  before_action :check_two_factor_authentication, except: :check_authentication
end
  • Related