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

fix(session): check for valid session before API calls #358

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

popietree
Copy link
Contributor

Description

Add condition session.token to execute checkSession only if session.token and other conditions are true. Otherwise 5 more API calls from getTotalUnprocessed, getTotalVerified, loadOrganizations, getTotal, and getTotalGrowerCount will fetch data.

Issue(s) addressed

What kind of change(s) does this PR introduce?

  • Enhancement
  • Bug fix
  • Refactor

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Issue

What is the current behavior?
The app does not logout properly upon 401 error. checkSession is not checking for valid session so logging in will trigger all API calls from mounting DashStat Components.

What is the new behavior?
App will logout and stop making API calls when JWT token changes.

Breaking change

Does this PR introduce a breaking change?
No

Other useful information

Currently no catch method on checkSession to logout on error. Does not handle 401 error specifically

@popietree popietree marked this pull request as ready for review February 4, 2022 01:57
@popietree popietree requested a review from a team as a code owner February 4, 2022 01:57
@popietree
Copy link
Contributor Author

Needs review

@@ -205,11 +205,11 @@ export const AppProvider = (props) => {
function checkSession() {
const localToken = JSON.parse(localStorage.getItem('token'));
const localUser = JSON.parse(localStorage.getItem('user'));
if (localToken && localUser) {

if (localToken && localUser && session.token) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unfortunately, this doesn't fix the underlying problem, and has the undesirable side effect of logging the user out every time they refresh the page.

We should call /auth/check_session if there's a local token and local user, but should ideally suspend other API calls until this check has finished and the user has been logged out (if the response is 401).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see.. I thought about putting a condition on the rendering of Dash Stat components. Should I change something in that API call function itself or its useEffect, or something in the error handling that is catching the API 401 errors?

By /auth/check_session do you mean the checkSession function that uses that path?

  • checkSession is currently called only once in the entire app in AppContext. Are you saying that checkSession should be called as it is below:

if (!user || !token) {
checkSession();
}

or that we should call checkSession if somewhere in the app there is local token and local user?
because that means we have to export checkSession as an app wide function with context

My solution to stop the API calls was to put a condition in the DashStats (the component responsible for making the API call upon mounting) The last time I tried, the API calls stopped. I'm not sure if that's the ideal way to suspend the API calls.

Does anyone have suggestions or want to try this ticket? I'm not sure the timeline. I would love to know the solution too.

@nmcharlton nmcharlton self-assigned this Mar 26, 2022
@nmcharlton
Copy link
Collaborator

This will likely be impacted by the introduction of Keycloak, so we'll pause development for now.

@nmcharlton nmcharlton marked this pull request as draft June 5, 2022 14:37
@nmcharlton nmcharlton added the on hold Awaiting progress on other items before a decision is made label Aug 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on hold Awaiting progress on other items before a decision is made
Projects
None yet
Development

Successfully merging this pull request may close these issues.

401 Unauthorized response to check_session doesn't trigger logout
2 participants