Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Panoptes authentication in all packages #4803

Merged
merged 19 commits into from
Jul 12, 2023

Conversation

eatyourgreens
Copy link
Contributor

@eatyourgreens eatyourgreens commented Jun 13, 2023

Refactor usePanoptesUser to get the current user from the stored Panoptes JWT (#4766.) Fall back to auth.checkCurrent(), which gets a new session and token from the Panoptes API, if we don't have a stored token.

Refactor the classifier to authenticate in ClassifierContainer, waiting until sign-in is complete and all user data has loaded before rendering the Classifier component (#4845 and #4875.) Move all the workflow validation up to ClassifierContainer, so that we validate the workflow ID (from the URL) when the user has loaded, then pass down null to Classifier if they don't have permission to view that workflow.

Test ClassifierContainer, checking that:

  • inactive workflows only load when you have permission to view them.
  • the mock API for /subjects/queued is only called once.

Fix a bug, in the project app, where updating the project user in the project app's store will unmount and mount the classifier (#4946.)

Fix a bug where a project_roles request is continually made (closes #4975).

The Classifier component uses MobX observable data, but isn't wrapped in observer, so I've also fixed that here.

Please request review from @zooniverse/frontend team or an individual member of that team.

Package

  • app-content-pages
  • app-project
  • lib-classifier

Linked Issue and/or Talk Post

How to Review

This PR reflects on the Panoptes JSON Web Token to get the logged-in user for the project app, the content pages app and the classifier, which should be faster than pinging the API for the current user session and won’t ask Panoptes OAuth to generate a new token (zooniverse/panoptes-javascript-client#53.) If there's no stored token yet, it falls back to auth.checkCurrent(), which is currently used for all user checks.

You should be able to use any project, logged-in or not, and continue to use Admin Mode without any changes to the app's behaviour.

On page load, there should only be one request to /api/me to get the user resource.

Subjects should only be fetched once, with your access token, after you log in (#4875.)

Apps will still lose your stored session when they unload eg. when going from PFE to FEM on the same origin, or when going from the project app to the content pages app. That can be tested in staging, on the https://frontend.preview.zooniverse.org origin, after this PR merges.

Checklist

PR Creator - Please cater the checklist to fit the review needed for your code changes.
PR Reviewer - Use the checklist during your review. Each point should be checkmarked or discussed before PR approval.

General

  • Tests are passing locally and on Github
  • Documentation is up to date and changelog has been updated if appropriate
  • You can yarn panic && yarn bootstrap or docker-compose up --build and FEM works as expected
  • FEM works in all major desktop browsers: Firefox, Chrome, Edge, Safari (Use Browserstack account as needed)
  • FEM works in a mobile browser

General UX

Example Staging Project: i-fancy-cats

  • All pages of a FEM project load: Home Page, Classify Page, and About Pages
  • Can submit a classification
  • Can sign-in and sign-out
  • The component is accessible

Refactoring

  • The PR creator has described the reason for refactoring
  • The refactored component(s) continue to work as expected

@eatyourgreens eatyourgreens added refactor Refactoring existing code performance labels Jun 13, 2023
@eatyourgreens eatyourgreens requested a review from a team June 13, 2023 15:00
@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 4 times, most recently from 77bb390 to 0b53f9c Compare June 15, 2023 08:21
@eatyourgreens eatyourgreens marked this pull request as ready for review June 15, 2023 08:21
@eatyourgreens eatyourgreens changed the title Get the user from Panoptes JWT tokens Project app: Get the user from the Panoptes JWT Jun 15, 2023
@coveralls
Copy link

coveralls commented Jun 15, 2023

Coverage Status

coverage: 81.95% (-0.1%) from 82.064% when pulling 0d5f5d0 on panoptes-token-auth into 42c767a on master.

@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 3 times, most recently from 6040321 to acc8d1c Compare June 15, 2023 21:27
@eatyourgreens eatyourgreens changed the title Project app: Get the user from the Panoptes JWT Authentication: Get the user from the Panoptes JWT Jun 15, 2023
@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 2 times, most recently from 83361af to 39de677 Compare June 16, 2023 11:16
@eatyourgreens eatyourgreens marked this pull request as draft June 16, 2023 11:35
@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 2 times, most recently from 67b8745 to a381330 Compare June 16, 2023 11:54
@eatyourgreens eatyourgreens marked this pull request as ready for review June 16, 2023 11:54
@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 3 times, most recently from 8097c25 to 287d5ce Compare June 17, 2023 06:11
@eatyourgreens eatyourgreens added the bug Something isn't working label Jun 17, 2023
@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Jun 17, 2023

Adding the 'bug' label to this because it fixes a small bug in the classifier. When you are already logged in, on the Classify page, usePanoptesUser in the classifier returns null when the Classifier component first loads (#4845.)

@eatyourgreens
Copy link
Contributor Author

You can try out a production build of this branch here:
https://fe-project-branch.preview.zooniverse.org/projects/zooniverse/gravity-spy

@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 2 times, most recently from 2d43bc2 to 39ac06e Compare June 20, 2023 15:44
Refactor `usePanoptesUser` to get the current user from the stored Panoptes JWT. Fall back to `auth.checkCurrent()`, which is slower, if we don't have a stored token.
Check Panoptes user state in `ClassifierContainer`. Defer rendering `Classifier` while that check is taking place.
Setting the UPP loading state to 'loading' will unmount the classifier, so avoid that after the page has loaded and the classifier is mounted.
Wait for all user session data to load before rendering the classifier. Refactor the user hooks to use the latest SWR API.
Copy link
Contributor

@goplayoutside3 goplayoutside3 left a comment

Choose a reason for hiding this comment

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

Looks good! I re-tested each of the Issues tagged here. The dev classifier runs as expected, as do the Zooniverse content pages and classifier storybook.

packages/app-project/stores/User/User.spec.js Outdated Show resolved Hide resolved
packages/lib-classifier/src/hooks/usePanoptesAuth.js Outdated Show resolved Hide resolved
@@ -39,7 +39,7 @@ const SubjectViewer = types

.views(self => ({
get disableImageToolbar () {
const subject = getRoot(self).subjects.active
const subject = tryReference(() => getRoot(self).subjects?.active)
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Just for my understanding, what does the change here implement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

subjects.active is a reference (it's actually the subject ID.) References should be accessed via tryReference to avoid errors.

https://mobx-state-tree.js.org/API/#tryreference

Copy link
Contributor Author

@eatyourgreens eatyourgreens Jul 11, 2023

Choose a reason for hiding this comment

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

The mock store errored on this line during cleanup, because subjects.active isn't always a valid reference when the store is destroyed.

@github-actions github-actions bot added the approved This PR is approved for merging label Jul 11, 2023
@eatyourgreens eatyourgreens force-pushed the panoptes-token-auth branch 2 times, most recently from e42ab19 to df1e425 Compare July 12, 2023 08:33
@eatyourgreens eatyourgreens enabled auto-merge (squash) July 12, 2023 08:46
@eatyourgreens eatyourgreens disabled auto-merge July 12, 2023 09:00
@eatyourgreens eatyourgreens merged commit 7d19264 into master Jul 12, 2023
@eatyourgreens eatyourgreens deleted the panoptes-token-auth branch July 12, 2023 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved This PR is approved for merging bug Something isn't working performance refactor Refactoring existing code
Projects
3 participants