The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

51 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. module Settings
  3. module TwoFactorAuthentication
  4. class ConfirmationsController < BaseController
  5. layout 'admin'
  6. before_action :authenticate_user!
  7. before_action :ensure_otp_secret
  8. skip_before_action :require_functional!
  9. def new
  10. prepare_two_factor_form
  11. end
  12. def create
  13. if current_user.validate_and_consume_otp!(confirmation_params[:code])
  14. flash.now[:notice] = I18n.t('two_factor_authentication.enabled_success')
  15. current_user.otp_required_for_login = true
  16. @recovery_codes = current_user.generate_otp_backup_codes!
  17. current_user.save!
  18. render 'settings/two_factor_authentication/recovery_codes/index'
  19. else
  20. flash.now[:alert] = I18n.t('two_factor_authentication.wrong_code')
  21. prepare_two_factor_form
  22. render :new
  23. end
  24. end
  25. private
  26. def confirmation_params
  27. params.require(:form_two_factor_confirmation).permit(:code)
  28. end
  29. def prepare_two_factor_form
  30. @confirmation = Form::TwoFactorConfirmation.new
  31. @provision_url = current_user.otp_provisioning_uri(current_user.email, issuer: Rails.configuration.x.local_domain)
  32. @qrcode = RQRCode::QRCode.new(@provision_url)
  33. end
  34. def ensure_otp_secret
  35. redirect_to settings_two_factor_authentication_path unless current_user.otp_secret
  36. end
  37. end
  38. end
  39. end