docs(session): require a user in the quick start and take login credentials from the body - #343
Merged
Merged
Conversation
…ntials from the body The Basic Usage /profile route read session.user through get_session, so a visitor with an anonymous session (one started by any write to request.session) got a 500 instead of a 401. /profile, the Full Example's profile, update and logout-all routes, and the migration /me snippet now use AuthenticatedSession / require_user_session. Every login example in SESSION.md and JWT_CLAIMS.md took username and password as query parameters, which puts passwords in URLs, browser history and access logs. They now take a LoginRequest pydantic body model. zh-TW translations updated to match. Closes #321
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The session docs had two problems a first-time user copies straight into their app (#321):
/profileroute usedDepends(get_session)and then readsession.user.user_id.get_sessionalso accepts anonymous sessions (session.user is None), which exist as soon as any route writes torequest.session, so such a visitor got a 500 (AttributeError) instead of a 401. The Full Example's profile, update and logout-all routes and the migration/mesnippet had the same pattern.username/passwordas query parameters, which puts passwords in URLs, browser history and access logs.Change
docs/SESSION.md/profileusesAuthenticatedSession; login takes aLoginRequestpydantic body model. The follow-up paragraph now points at/profileinstead of repeating a separate/accountsnippet, and notes that/logoutonly needsget_session.LoginRequest;/api/user/profile,/api/user/updateand/api/auth/logout-alluseAuthenticatedSession. Routes that never readsession.user(/api/messages,/api/auth/logout) keepget_session./mesnippet usesrequire_user_session, which is added to the list of dependencies that work under either middleware.SessionManagerDeplogin example takesLoginRequestinstead of ausernamequery parameter.docs/JWT_CLAIMS.md: the complete application example's/auth/logintakes aLoginRequestbody model.i18n/zh-TW/docs/SESSION.md,i18n/zh-TW/docs/JWT_CLAIMS.md: the same changes.README.md, the other docs andexamples/*.pywere checked:examples/session_login.pyandexamples/session_jwt.pyalready take a pydantic body, and no other page has a query-parameter login or readssession.userthroughget_session. Docs only, so no changelog fragment.Verification
fastapi.testclientagainst this branch's library (memory backend substituted for Redis; the JWT example without the custom multi-tenant serializer):request.session) gets 401 from/profile,/api/user/profile,/api/user/update,/api/auth/logout-alland/me; no session also gets 401;uv run pytest -q,uv run ruff check,uv run ruff format --checkanduv run pre-commit run --all-filespass.Closes #321