-
Notifications
You must be signed in to change notification settings - Fork 0
feat(auth): handle refresh token #67
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements comprehensive OIDC authentication with automatic token refresh functionality using Better Auth. The implementation adds encrypted token storage in HTTP-only cookies, automatic access token refresh when expired, and a dedicated refresh endpoint.
Key changes:
- Custom token management with AES-256-GCM encryption for access and refresh tokens
- Automatic token refresh flow via
/api/auth/refresh-tokenendpoint - Server-side authenticated API client with transparent token handling
- Database hooks to persist tokens on both initial login and re-login flows
Reviewed Changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
src/lib/auth/types.ts |
New TypeScript interfaces for OIDC token data and responses |
src/lib/auth/utils.ts |
Encryption/decryption utilities using JWE with type guards |
src/lib/auth/constants.ts |
Centralized auth configuration and environment variables |
src/lib/auth/auth.ts |
Core auth logic including Better Auth config, token refresh, and cookie management |
src/lib/auth/token.ts |
Token retrieval and refresh orchestration server actions |
src/lib/auth/README.md |
Comprehensive architecture documentation with flow diagrams |
src/app/api/auth/refresh-token/route.ts |
API route handler for token refresh requests |
src/lib/api-client.ts |
Authenticated API client factory with automatic token injection |
src/app/catalog/actions.ts |
Updated to use authenticated client |
src/app/layout.tsx |
Removed unnecessary api-client import |
src/lib/auth/__tests__/auth.test.ts |
Updated tests for token management without cookie deletion |
src/lib/auth/__tests__/token.test.ts |
Comprehensive test suite for token refresh flows |
vitest.config.mts |
Added OIDC_PROVIDER_ID environment variable for tests |
src/mocks/server.ts |
Removed biome-ignore comments (now handled by config) |
src/mocks/mocker.ts |
Code formatting improvements for long strings |
biome.json |
Updated schema version and disabled console warnings for mocks |
dev-auth/README.md |
Updated environment variable documentation |
CLAUDE.md |
Added detailed auth environment variable documentation |
AGENTS.md |
Added auth configuration details for AI agents |
- Add Better Auth with OIDC (OAuth2 Authorization Code Flow) - Implement custom token storage in encrypted HTTP-only cookies - Add automatic access token refresh when expired - Create dedicated /api/auth/refresh-token endpoint - Fix race condition by creating new client instance per request - Add fail-fast validation for API_BASE_URL env var - Add refresh token expiration check before API call - Add warning when provider doesn't return new refresh token - Add account.update.after hook for re-login flow - Add comprehensive auth documentation in src/lib/auth/README.md - Add 22 unit tests for auth flows Security improvements: - AES-256-GCM encryption for token storage - Server-side only API client (no token exposure to client) - HTTP-only cookies (not accessible via JavaScript) - SameSite protection - 7-day session management with Better Auth cookie cache Fixes #50
Authentication Implementation
Kapture.2025-11-20.at.18.32.32.mp4
Implemented OIDC authentication with Better Auth and custom token management:
Key Features:
/api/auth/refresh-tokenendpoint for token refreshSee
src/lib/auth/README.mdfor detailed architecture and flow diagrams.Refresh token
Authentication flow