Skip to content

Conversation

@floehopper
Copy link
Contributor

@floehopper floehopper commented Apr 14, 2025

This is a bit of an edge case, but if the user is logged in and then the session is reset (or at least its current_user value is reset) and RpiAuth::Controllers::CurrentUser#current_user is called within the same request, then previously it would have returned the user "cached" in the @current_user. Now it will return nil which seems less surprising.

This was highlighted by this example in code-club-frontend. In that case the example passes, because that app has its own implementation of #current_user. However, when I added a similar example in experience-cs where we're using RpiAuth::Controllers::CurrentUser#current_user, the example failed.

I've added a rather contrived example by adding a new HomeController#reset_user action to the dummy app used in the specs. However, hopefully it helps explain a bit about why the change is necessary.

It looks as if the only two RPF repos that use RpiAuth::Controllers::CurrentUser are experience-ai here and here and experience-cs. I'm pretty confident this change won't affect either of those apps, so this can probably be a minor version bump.

I think a possible alternative to making this change would be to provide a #reset_user method which could both reset the session and reset the ivar-cached user. However, given that the code-club-frontend implementation already uses this approach, I think that probably makes more sense for now.

This is a bit of an edge case, but if the user is logged in and then the
session is reset (or at least its `current_user` value is reset) and
`RpiAuth::Controllers::CurrentUser#current_user` is called within the
*same* request, then previously it would have returned the user "cached"
in the `@current_user`. Now it will return `nil` which seems less
surprising.

This was highlighted by this example [1] in code-club-frontend. In that
case the example passes, because that app has its own implementation of
`#current_user` [2]. However, when I added a similar example in
experience-cs where we're using
`RpiAuth::Controllers::CurrentUser#current_user`, the example failed.

I've added a rather contrived example by adding a new
`HomeController#reset_user` action to the dummy app used in the specs.
However, hopefully it helps explain a bit about why the change is
necessary.

It looks as if the only two RPF repos that use
`RpiAuth::Controllers::CurrentUser` are experience-ai [3,4] and
experience-cs [5]. I'm pretty confident this change won't affect either
of those apps, so this can probably be a minor version bump.

I think a possible alternative to making this change would be to provide
a `#reset_user` method which could both reset the session *and* reset
the ivar-cached user. However, given that the code-club-frontend
implementation already uses this approach, I think that probably makes
more sense for now.

[1]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/901f2200e926741fd170858286d64f1dd49994b4/spec/requests/refresh_credentials_spec.rb#L90
[2]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/901f2200e926741fd170858286d64f1dd49994b4/app/controllers/concerns/authentication_concern.rb#L6-L11
[3]: https://github.com/RaspberryPiFoundation/experience-ai/blob/ad729cefe060fcc0f0c345b3297247069fe55867/app/controllers/application_controller.rb#L6
[4]: https://github.com/RaspberryPiFoundation/experience-ai/blob/ad729cefe060fcc0f0c345b3297247069fe55867/app/controllers/api/v1/zipped_resources_controller.rb#L6
[5]: https://github.com/RaspberryPiFoundation/experience-cs/blob/33fa4756371fa3a550f32ac7613130e0925c17d3/app/controllers/application_controller.rb#L4
@github-actions
Copy link

Test coverage: 100.0%

Copy link
Contributor

@patch0 patch0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the detailed PR notes. Makes a lot of sense.

@floehopper floehopper merged commit 14f3b76 into main Apr 14, 2025
13 checks passed
@floehopper floehopper deleted the check-session-for-current-user-before-ivar-cache branch April 14, 2025 15:01
floehopper added a commit that referenced this pull request Apr 15, 2025
floehopper added a commit that referenced this pull request Apr 15, 2025
- Add `CHANGELOG` entry for #79
- Update `CHANGELOG` & `RpiAuth::VERSION` in preparation to release v4.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants