Skip to content

Instantly share code, notes, and snippets.

@skorfmann
Last active December 14, 2015 21:19
Show Gist options
  • Save skorfmann/5150444 to your computer and use it in GitHub Desktop.
Save skorfmann/5150444 to your computer and use it in GitHub Desktop.
Track authentication events from devise (warden)
# this gets called every time a user is authenticated
# either via an actual sign in or through a cookie
Warden::Manager.after_authentication do |user,auth,opts|
auth.params.has_key?('user') ? 'trigger direct sign in' : 'trigger cookie sign in'
end
# this gets called every time a user is fetched from session.
# happens on every request!
Warden::Manager.after_fetch do |user,auth,opts|
'trigger user request'
end
# this gets called every time a user performs a sign out.
Warden::Manager.before_logout do |user,auth,opts|
'trigger sign out'
end
# this gets called every time a request fails due to lacking authentication
Warden::Manager.before_failure do |env, opts|
# parse params
# Rack::Request.new(env).params
# authentication failed:
# opts == {:scope=>:user, :recall=>"devise/sessions#new", :action=>"unauthenticated", :attempted_path=>"/users/sign_in"}
# redirect as a user is required
# opts == {:scope=>:user, :action=>"unauthenticated", :attempted_path=>"/"}
'trigger sign in failed' if opts.has_key?(:recall)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment