Skip to content

Commit

Permalink
Support clearance similar to devise/warden
Browse files Browse the repository at this point in the history
Clearance is a fairly popular alternative. https://github.com/thoughtbot/clearance
  • Loading branch information
jrochkind committed Aug 16, 2016
1 parent b76aa66 commit 6910b8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/bugsnag/middleware/clearance_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Bugsnag::Middleware
class ClearanceUser
COMMON_USER_FIELDS = [:email, :name, :first_name, :last_name, :created_at, :id]

def initialize(bugsnag)
@bugsnag = bugsnag
end

def call(notification)
if notification.request_data[:rack_env] &&
notification.request_data[:rack_env]["clearance"] &&
notification.request_data[:rack_env]["clearance"].signed_in? &&
notification.request_data[:rack_env]["clearance"].current_user

# Extract useful user information
user = {}
user_object = notification.request_data[:rack_env]["clearance"].current_user
if user_object
# Build the user info for this scope
COMMON_USER_FIELDS.each do |field|
user[field] = user_object.send(field) if user_object.respond_to?(field)
end
end

# We merge the first warden scope down, so that it is the main "user" for the request
notification.user = user unless user.empty?
end

@bugsnag.call(notification)
end
end
end
1 change: 1 addition & 0 deletions lib/bugsnag/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(app)
# Hook up rack-based notification middlewares
config.middleware.insert_before([Bugsnag::Middleware::Rails3Request,Bugsnag::Middleware::Callbacks], Bugsnag::Middleware::RackRequest) if defined?(::Rack)
config.middleware.insert_before(Bugsnag::Middleware::Callbacks, Bugsnag::Middleware::WardenUser) if defined?(Warden)
config.middleware.insert_before(Bugsnag::Middleware::Callbacks, Bugsnag::Middleware::ClearanceUser) if defined?(Clearance)

Bugsnag.configuration.app_type ||= "rack"
end
Expand Down

0 comments on commit 6910b8d

Please sign in to comment.