Module: Honeycomb::Warden

Included in:
Rack::Middleware, Rails::Middleware, Sinatra::Middleware
Defined in:
lib/honeycomb/integrations/warden.rb

Overview

Methods for extracting common warden/devise fields from a rack env hash

Constant Summary collapse

COMMON_USER_FIELDS =
%i[
  email
  name
  first_name
  last_name
  created_at
  id
].freeze
SCOPE_PATTERN =
/^warden\.user\.([^.]+)\.key$/.freeze

Instance Method Summary collapse

Instance Method Details

#extract_user_information(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/honeycomb/integrations/warden.rb', line 17

def extract_user_information(env)
  warden = env["warden"]

  return unless warden

  session = env["rack.session"] || {}
  keys = session.keys.select do |key|
    key.match(SCOPE_PATTERN)
  end
  warden_scopes = keys.map do |key|
    key.gsub(SCOPE_PATTERN, "\\1")
  end
  best_scope = warden_scopes.include?("user") ? "user" : warden_scopes.first

  return unless best_scope

  env["warden"].user(scope: best_scope, run_callbacks: false).tap do |user|
    COMMON_USER_FIELDS.each do |field|
      user.respond_to?(field) && yield("user.#{field}", user.send(field))
    end
  end
end