-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure Current.user is always up-to-date w/ Devise
Previously, if a user was signed in or out in the *middle* of an action, the value of `Current.user` was not updated. This makes use of `Warden::Manager.after_set_user` [1] and `Warden::Manager.before_logout` [2] hooks to update `Current.user`. I'm using `Devise::Controllers::SignInOut#sign_in` [3] & `Devise::Controllers::SignInOut#sign_out` within the tests, because I believe that's what you're supposed to use when using `Devise` and indeed it looks like that's what we're using in e.g. `ConfirmationsController`, `Devise::TwoStepVerificationController` & `Devise::TwoStepVerificationSessionController`. I've added the `Warden::Manager` hooks within `Devise::Hooks::SetCurrentUser` and required it from the top of `app/controllers/application_controller.rb` to be consistent what we're already doing with `Devise::Hooks::TwoStepVerification`, although it seems a bit odd/non-idiomatic to execute some code via a call to `require`. Also it strikes me as slightly odd that the hooks are in a top-level `Devise` module despite being associated with `Warden`, although I suppose `Warden` is only there because it's a dependency of `Devise`. [1]: https://www.rubydoc.info/gems/warden/1.2.9/Warden/Hooks#after_set_user-instance_method [2]: https://www.rubydoc.info/gems/warden/1.2.9/Warden/Hooks#before_logout-instance_method [3]: https://www.rubydoc.info/github/plataformatec/devise/Devise%2FControllers%2FSignInOut:sign_in [4]: https://www.rubydoc.info/github/plataformatec/devise/Devise%2FControllers%2FSignInOut:sign_out
- Loading branch information
1 parent
87c16af
commit 9114bdb
Showing
3 changed files
with
83 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Devise::Hooks::SetCurrentUser | ||
Warden::Manager.after_set_user(only: :set_user) do |user, _auth, _options| | ||
Current.user = user | ||
end | ||
Warden::Manager.before_logout do |_user, _auth, _options| | ||
Current.user = nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters