Class: Authform::Rails::Concern

Inherits:
Module
  • Object
show all
Defined in:
lib/authform/rails/concern.rb

Instance Method Summary collapse

Constructor Details

#initialize(form_uuid:) ⇒ Concern

Returns a new instance of Concern.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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