feat(auth): support avatar file upload and enforce gmail registration
Overview
This PR enhances the authentication module with two key requirements:
-
Gmail Registration Restriction: Enforces that all newly registered accounts must use a
@gmail.comemail address. -
Avatar File Upload Pipeline: Transitions avatar updates from manual URL inputs to direct image file uploads (
multipart/form-data), backed by the abstract storage layer (IStorageService).
Key Changes
1. Gmail Domain Enforcement
- Updated
registerSchemawith validation ensuring email addresses end with@gmail.com(case-insensitive). - Added unit tests covering standard Gmail addresses, sub-addressing (
+tag), and rejection of non-Gmail domains.
2. Avatar Upload Middleware
- Integrated
multerwith memory storage. - Added
uploadAvatarMiddlewareenforcing a 5MB maximum file size and strict image MIME type validation (image/jpeg,image/png,image/webp,image/gif), throwing standardizedAppError(422VALIDATION_ERROR).
3. Service & Storage Integration
-
AuthService.uploadAvatar:- Streams image buffers directly to
IStorageServiceunderavatars/<userId>-<timestamp>.<ext>. - Automatically cleans up obsolete avatar files from storage when a new avatar is uploaded.
- Updates the user record in PostgreSQL and returns the updated user profile.
- Streams image buffers directly to
-
AuthService.getAvatarStream:- Validates and securely reads stored avatar files with MIME detection for HTTP streaming.
-
Profile Update Decoupling:
- Removed
avatarUrlfromUpdateMeDtoandupdateMeSchema. ThePATCH /api/v1/auth/meendpoint now strictly handles profile fields (fullName).
- Removed
4. Endpoints & Audit Logging
-
POST /api/v1/auth/avatar: Authenticated multipart upload endpoint. -
GET /api/v1/auth/avatar/:fileName: Public endpoint serving uploaded avatars. - Added
UPDATE_AVATARaction toAUDIT_ACTIONS.