Skip to content

Commit 51ce6a9

Browse files
authored
Merge pull request #850 from SuzukiRyuichiro/current-user-for-rails-8-auth
Add documentation to provide a guide for how to use pundit with rails 8's built-in authentication generator
2 parents 5507430 + b117d41 commit 51ce6a9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add `Pundit::Authorization#pundit_reset!` hook to reset the policy and policy scope cache. (#830)
88
- Add links to gemspec. (#845)
99
- Register policies directories for Rails 8 code statistics (#833)
10+
- Added an example for how to use pundit with Rails 8 authentication generator (#850)
1011

1112
## Changed
1213

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,17 @@ def pundit_user
582582
User.find_by_other_means
583583
end
584584
```
585+
586+
For instance, Rails 8 includes a built-in [authentication generator](https://github.com/rails/rails/tree/8-0-stable/railties/lib/rails/generators/rails/authentication). If you choose to use it, the currently logged-in user is accessed via `Current.user` instead of `current_user`.
587+
588+
To ensure compatibility with Pundit, define a `pundit_user` method in `application_controller.rb` (or another suitable location) as follows:
589+
590+
```ruby
591+
def pundit_user
592+
Current.user
593+
end
594+
```
595+
585596
### Handling User Switching in Pundit
586597

587598
When switching users in your application, it's important to reset the Pundit user context to ensure that authorization policies are applied correctly for the new user. Pundit caches the user context, so failing to reset it could result in incorrect permissions being applied.

0 commit comments

Comments
 (0)