diff --git a/CHANGELOG.md b/CHANGELOG.md index f1f8299a..3edf4d6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +- Add `before_logout` hook to Warden that calls `ahoy.reset` + ## 5.0.2 (2023-10-05) - Excluded visits from Rails health check diff --git a/README.md b/README.md index c1a0050f..a1ba7b1f 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ visitable :sign_up_visit ### Users -Ahoy automatically attaches the `current_user` to the visit. With [Devise](https://github.com/heartcombo/devise), it attaches the user even if they sign in after the visit starts. +Ahoy automatically attaches the `current_user` to the visit. With [Devise](https://github.com/heartcombo/devise), it attaches the user even if they sign in after the visit starts. It also resets the tracker's associated user after they sign out. With other authentication frameworks, add this to the end of your sign in method: @@ -220,6 +220,12 @@ With other authentication frameworks, add this to the end of your sign in method ahoy.authenticate(user) ``` +Add this to the end of your sign out method: + +```ruby +ahoy.reset +``` + To see the visits for a given user, create an association: ```ruby diff --git a/lib/ahoy/warden.rb b/lib/ahoy/warden.rb index a15c1600..1fb2ea3f 100644 --- a/lib/ahoy/warden.rb +++ b/lib/ahoy/warden.rb @@ -3,3 +3,9 @@ ahoy = Ahoy::Tracker.new(request: request) ahoy.authenticate(user) end + +Warden::Manager.before_logout do |_, auth, _| + request = ActionDispatch::Request.new(auth.env) + ahoy = Ahoy::Tracker.new(request: request) + ahoy.reset +end