Skip to content

[PUI] Login / Logout State Fixes - #6368

Merged
SchrodingersGat merged 31 commits into
inventree:masterfrom
SchrodingersGat:url-patch
Feb 2, 2024
Merged

SchrodingersGat merged 31 commits into
inventree:masterfrom
SchrodingersGat:url-patch

Conversation

@SchrodingersGat

@SchrodingersGat SchrodingersGat commented Jan 30, 2024 •

Copy link
Copy Markdown
Member

Fixes some bugs related to state management and API queries.

This drastically improves the login/logout experience for platform UI

@SchrodingersGat SchrodingersGat added bug Identifies a bug which needs to be addressed User Interface Related to the frontend / User Interface labels Jan 30, 2024
@SchrodingersGat SchrodingersGat added this to the 0.14.0 milestone Jan 30, 2024
@netlify

netlify Bot commented Jan 30, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for inventree-web-pui-preview ready!

Name Link
🔨 Latest commit d757b11
🔍 Latest deploy log https://app.netlify.com/sites/inventree-web-pui-preview/deploys/65bc3a24c05188000807e70a
😎 Deploy Preview https://deploy-preview-6368--inventree-web-pui-preview.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 86 (no change from production)
Best Practices: 100 (🟢 up 8 from production)
SEO: 70 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@matmair matmair left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread InvenTree/InvenTree/api.py Resolved
Comment thread src/frontend/src/functions/auth.tsx Resolved

@matmair matmair left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM; we will need to bump the API version

@SchrodingersGat

Copy link
Copy Markdown
Member Author

LGTM; we will need to bump the API version

There are no longer any API changes in this PR :)

- Using new get_token_from_request method
- return early
- More fixes for incorrect api calls
- track an internal flag in apiState
Comment thread src/frontend/src/functions/auth.tsx Resolved
- Rename doClassicLogin to doBasicLogin (reflecting "basic" auth)
- Add "loggedIn" attribute to sessionState
- Cleanup procedure for securing a token
- Prevent failed calls to user_me
- Simpler to just track token state
- No need for separate status tracker
- Works much cleaner this way
@SchrodingersGat

Copy link
Copy Markdown
Member Author

Ok, it took me a while to be happy with this - I had been seeing lots of strange login / logout bugs up until now. I am pretty sure this is good now, but I'll leave open for you guys to check again, make sure we are all happy with it.

Comment thread src/frontend/src/functions/auth.tsx Resolved
Comment thread src/frontend/src/functions/auth.tsx Resolved
Comment thread src/frontend/src/states/UserState.tsx Resolved
Comment thread src/frontend/src/functions/auth.tsx Resolved
- Account for the presence of a CSRF session cookie
- If available, use it to fetch a token
- Forces logout from session
- Tested, works well!
- Clean up notifications

@matmair matmair left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have submitted a fix, now it works

@SchrodingersGat SchrodingersGat changed the title [PUI] State and URL fixes [PUI] Login / Logout State Fixes Feb 1, 2024
@SchrodingersGat

Copy link
Copy Markdown
Member Author

@matmair this review process has made me question - is it a good idea to just use CSRF session auth for PUI (as we do for CUI) and not use API tokens at all? As far as I can see there are two major advantages:

Session Management

Using a single session (based on CSRF token) means that CUI and PUI are in sync. No need to manage two separate login methods (session token / API token) as we do now.

File Downloads

Moving to session auth would allow us to download files / reports / etc in the browser - a problem which is currently hard to solve with API tokens - see #5697

So, move to session token and this problem goes away (because the web session has the same token and we don't have to work out how to pass an API token to a file download).


Thoughts on this? Now that we are deep in the weeds on this is the right time to do it. I'm just not sure how to get the CSRF token into the session when you login from PUI - we have to replicate the CUI behaviour...

@matmair

matmair commented Feb 1, 2024

Copy link
Copy Markdown
Member

@SchrodingersGat that would be a possibility. I'm not 100% if it would work but it would be cleaner for sure.
It would require API changes as we are not exposing db session information to the API right now and probably also adaption of the login/logout flow.

From my POV that would make more sense in a new PR as this is one is blocking #6333, which is blocking #6200

@matmair

matmair commented Feb 1, 2024

Copy link
Copy Markdown
Member

And a small technical nitpick: Session managment is handled via the session token, not the CSRF one - those are similar but different mechanics.

@SchrodingersGat

Copy link
Copy Markdown
Member Author

Would a session-based management make SSO support easier, though? Should we consider this properly first before merging SSO, so that we don't have to potentially refactor that again anyway?

@SchrodingersGat

SchrodingersGat commented Feb 1, 2024 •

Copy link
Copy Markdown
Member Author

And a small technical nitpick: Session managment is handled via the session token, not the CSRF one - those are similar but different mechanics.

Ok, maybe my understanding is a bit off here. I found that using just this CSRF token cookie I was able to login:

image

@matmair

matmair commented Feb 1, 2024 •

Copy link
Copy Markdown
Member

Would a session-based management make SSO support easier, though? Should we consider this properly first before merging SSO, so that we don't have to potentially refactor that again anyway?

SSO is already fully functional and done in #6333 - it works using the backend

@matmair

matmair commented Feb 1, 2024

Copy link
Copy Markdown
Member

And a small technical nitpick: Session managment is handled via the session token, not the CSRF one - those are similar but different mechanics.

Ok, maybe my understanding is a bit off here. I found that using just this CSRF token cookie I was able to login:

image

Login yes but full Django sessions use a separate cookie - see the offical docs. Authentication and Sessions are fully decoupled.

@SchrodingersGat

Copy link
Copy Markdown
Member Author

Ok, so should I merge this in (so we can proceed with SSO) and then investigate session based login for PUI separately?

@SchrodingersGat
SchrodingersGat merged commit f97cdef into inventree:master Feb 2, 2024
@SchrodingersGat
SchrodingersGat deleted the url-patch branch February 2, 2024 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Identifies a bug which needs to be addressed User Interface Related to the frontend / User Interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants