Add JWT authentication and user profile endpoints with bcrypt password hashing #1
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.
Overview
This PR implements secure JWT-based authentication and user profile features for the Language Tutor API, enabling users to log in and access their profile information securely.
Changes
New API Endpoints
POST /login- User authentication endpointGET /me- User profile endpoint (protected)POST /register- Enhanced user registrationSecurity Implementation
passlibfor all new user registrations, with automatic salt generationpython-josewith HS256 algorithmOAuth2PasswordBearerandOAuth2PasswordRequestFormget_current_user()dependency that verifies JWT tokens and loads user data for protected endpointsTechnical Details
Dependencies Added:
passlib==1.7.4- Industry-standard password hashingpython-jose[cryptography]==3.5.0- JWT token handlingsqlmodel==0.0.27- SQLModel ORMKey Functions:
hash_password()- Securely hashes passwords using bcryptverify_password()- Verifies passwords with fallback for legacy hashescreate_access_token()- Generates JWT tokens with expirationget_current_user()- FastAPI dependency for authenticationConfiguration:
SECRET_KEYfrom environment variables for JWT signingDocumentation
Comprehensive documentation has been added to help developers get started:
Testing
test_auth.pywith comprehensive unit tests (4/4 passing)examples.pydemonstrating complete authentication flowBackward Compatibility
✅ Zero breaking changes - All existing endpoints continue to work:
/languagesand/docs-sourceendpoints work as beforeExample Usage
Security Notes
The implementation follows security best practices:
Production Recommendations:
Interactive Documentation
All endpoints are fully documented in the auto-generated Swagger UI at
http://localhost:8000/docs, including:Testing Instructions
See
TESTING.mdfor detailed manual testing instructions.Closes: #[issue-number]
Note: As requested, no
/logoutendpoint has been implemented. JWT tokens naturally expire after 30 minutes, and clients can simply discard the token to log out.Original prompt
This pull request was created as a result of the following prompt from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.