feat: Replace AWS Amplify with Direct Cognito OAuth Implementation#545
Open
DerekRoberts wants to merge 5 commits intomainfrom
Open
feat: Replace AWS Amplify with Direct Cognito OAuth Implementation#545DerekRoberts wants to merge 5 commits intomainfrom
DerekRoberts wants to merge 5 commits intomainfrom
Conversation
Replace AWS Amplify dependency with lightweight direct Cognito OAuth implementation, reducing bundle size by ~39MB and removing 155 packages while maintaining full authentication functionality including automatic token refresh. Key changes: - Create CognitoAuthService with OAuth flow, token exchange, and cookie management - Implement automatic token refresh (every 2 minutes) matching Amplify behavior - Update AuthProvider to use CognitoAuthService instead of Amplify - Remove aws-amplify dependency and all related packages - Update tests to mock CognitoAuthService - Update documentation to reflect new implementation Benefits: - Reduced dependencies: 155 packages removed - Smaller bundle: ~140-190KB reduction in production bundle - Security: 2 low-severity vulnerabilities removed - Maintenance: No external auth package updates needed - Full feature parity: Session management, auto-refresh, OAuth flows Session management: - Automatic token refresh every 2 minutes (matches Amplify) - Tokens refreshed when expiring within 5 minutes - Seamless session persistence using refresh tokens - Background refresh maintains user sessions
Document that setup convenience and session management are both maintained or improved in the direct Cognito OAuth implementation.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR replaces the AWS Amplify dependency with a lightweight direct Cognito OAuth implementation to reduce bundle size (removing 155 packages and ~140-190 KB gzipped) and dependencies while maintaining authentication functionality including automatic token refresh.
Changes:
- Implemented
CognitoAuthServicefor direct OAuth integration with Cognito including authorization flow, token exchange, cookie management, and automatic token refresh - Updated
AuthProviderto useCognitoAuthServiceinstead of AWS Amplify APIs - Removed AWS Amplify dependencies from package.json and related configuration files
Reviewed changes
Copilot reviewed 12 out of 14 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
| frontend/vite.config.ts | Removed amplifyconfiguration.ts from test exclusions |
| frontend/src/types/amplify.ts | Replaced Amplify JWT type import with custom JWT interface |
| frontend/src/services/CognitoAuthService.ts | New service implementing OAuth flow, token management, and automatic refresh |
| frontend/src/module.d.ts | Removed aws-amplify module declaration |
| frontend/src/index.tsx | Removed Amplify configuration and CookieStorage setup |
| frontend/src/contexts/AuthProvider.tsx | Updated to use CognitoAuthService with OAuth callback handling and token refresh interval |
| frontend/src/amplifyconfiguration.ts | Deleted configuration file (no longer needed) |
| frontend/src/tests/services/CognitoAuthService.test.ts | New comprehensive test suite for CognitoAuthService |
| frontend/src/tests/index.test.tsx | Removed Amplify-related test mocks and assertions |
| frontend/src/tests/contexts/AuthProvider.test.tsx | Updated mocks to use CognitoAuthService instead of Amplify |
| frontend/package.json | Removed aws-amplify dependency |
| docs/COOKIE_SECURITY.md | Updated documentation to reflect direct Cognito OAuth implementation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Handle optional refresh_token in token refresh response (preserve existing if not returned) - Use refreshed token directly instead of reading from cookies (fixes race condition) - Clean up OAuth code from URL immediately after extraction (security) - Add JWT validation before decoding in storeTokens() - Sanitize error messages (don't expose sensitive info in production) - Fix cookie Secure flag formatting (remove trailing space) - Extract baseCookieName to method (reduce magic strings) - Fix test JWT format (use proper JWT structure) - Fix deprecated substr() in tests - Update documentation date All critical security and correctness issues addressed.
refreshUserState() already calls loadUserToken() internally, so calling it twice was redundant. This eliminates duplicate token retrieval and parsing, making the code cleaner and more efficient for reuse in other projects. Benefits: - Eliminates duplicate getTokens() calls - Reduces unnecessary cookie reads - Cleaner pattern for reuse - Better performance
Add comprehensive error handling tests: - Token refresh failure (non-OK response from endpoint) - Malformed JWT tokens that can't be parsed - Tokens without expiration claims - Token exchange failures during handleCallback - Network errors during token exchange All tests passing. Improves test coverage for error paths.
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.
Overview
This PR replaces the AWS Amplify dependency with a lightweight direct Cognito OAuth implementation, significantly reducing bundle size and dependencies while maintaining full authentication functionality including automatic token refresh.
Changes
Core Implementation
CognitoAuthServicewith complete OAuth flowUpdated Components
AuthProviderto useCognitoAuthServiceinstead of Amplifyindex.tsxamplifyconfiguration.ts(no longer needed)Testing
AuthProvidertests to mockCognitoAuthServiceindex.test.tsxto remove Amplify mocksCognitoAuthService.test.tswith test coverageDependencies
aws-amplifyfrompackage.jsonpackage-lock.json(155 packages removed)Documentation
COOKIE_SECURITY.mdto reflect new implementationBenefits
Bundle Size Reduction
Security
Maintenance
Session Management
✅ Automatic token refresh is fully implemented and matches AWS Amplify behavior:
Testing
All existing tests have been updated and new tests added:
Migration Notes
Verification Checklist
Related
Addresses the concern about losing session manager functionality - automatic token refresh is fully implemented and matches Amplify's behavior.
Thanks for the PR!
Deployments, as required, will be available below:
Please create PRs in draft mode. Mark as ready to enable:
After merge, new images are deployed in: