class Authform::Rails::Concern

Public Class Methods

new(form_uuid:) click to toggle source
Calls superclass method
# File lib/authform/rails/concern.rb, line 4
def initialize(form_uuid:)
  super() do
    extend ActiveSupport::Concern

    included do
      helper_method :current_user
      before_action :_exchange_voucher_for_user
    end

    private

    define_method :current_user do
      begin
        JSON.parse(cookies[authform_user_cookie_key])["data"]
      rescue
        nil
      end
    end

    define_method :_exchange_voucher_for_user do
      if params[:_authformVoucher]
        response = Faraday.get("https://api.authform.io/v1/exchangeVoucherForUser.json", { formUuid: form_uuid, voucher: params[:_authformVoucher] })

        if response.status == 200
          cookies[authform_user_cookie_key] = response.body
        end

        q = Rack::Utils.parse_query(URI.parse(request.url).query)
        q.delete("_authformVoucher")
        url = q.empty? ? request.path : "#{request.path}?#{q.to_query}"

        redirect_to url, status: 302 # redirect to finish removal of query param
      end
    end

    define_method :authform_user_cookie_key do
      "user#{form_uuid}"
    end
  end
end