A comprehensive profile update system with Cloudinary avatar upload functionality, built with React, Redux, Node.js, Express, and MongoDB.
- Profile Update: Update name, username, and bio
- Avatar Upload: Upload images to Cloudinary with automatic optimization
- File Validation: Image type and size validation (5MB limit)
- Username Uniqueness: Check for duplicate usernames
- Data Sanitization: Trim whitespace and validate inputs
- Error Handling: Comprehensive error handling and validation
- Old Avatar Cleanup: Automatically delete old avatars from Cloudinary
- Beautiful UI: Modern, responsive design with Tailwind CSS
- Real-time Validation: Client-side validation with error messages
- Image Preview: Live preview of selected avatar
- Loading States: Visual feedback during operations
- File Upload: Drag & drop or click to upload
- Change Detection: Only allow save when changes are made
- Error Recovery: Clear error messages and retry options
Backend/src/
βββ config/
β βββ cloudinary.js # Cloudinary configuration
βββ middleware/
β βββ upload.js # Multer & Cloudinary upload middleware
βββ controllers/
β βββ userCotroller.js # Profile update controllers
βββ Routes/
β βββ userRoute.js # Profile routes
βββ models/Authmodel/
βββ User.js # Updated User model with username
Frontend/src/
βββ pages/
β βββ EditProfile.jsx # Profile edit component
β βββ Profile.jsx # Updated profile view
βββ redux/
β βββ userSlice.jsx # Profile update Redux actions
βββ Components/
βββ Routings.jsx # Updated with edit profile route
cd Backend
npm install cloudinaryCreate a .env file in the Backend directory:
# Database
MONGODB_URI=mongodb://localhost:27017/hackathon
# JWT
JWT_SECRET=your_jwt_secret_key_here
# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret
# Server
PORT=5000
NODE_ENV=development- Sign up at Cloudinary
- Get your Cloud Name, API Key, and API Secret from the dashboard
- Add them to your
.envfile
No additional dependencies required - all packages are already installed.
// Get user profile (public)
GET /api/users/:id
// Update profile (without avatar)
PUT /api/users/me
Body: { name, username, bio }
// Update profile with avatar
PUT /api/users/me/avatar
Body: FormData with name, username, bio, avatar filePUT /api/users/me
Content-Type: application/json
Authorization: Bearer <token>
{
"name": "John Doe",
"username": "johndoe",
"bio": "Software developer passionate about web technologies"
}PUT /api/users/me/avatar
Content-Type: multipart/form-data
Authorization: Bearer <token>
FormData:
- name: "John Doe"
- username: "johndoe"
- bio: "Software developer passionate about web technologies"
- avatar: <image file>- Navigate to your profile page:
/profile/:id - Click the edit icon (pencil) next to your name
- You'll be redirected to
/edit-profile
- Avatar Upload: Click on the avatar or "Upload Avatar" button
- Name: Update your full name (2-50 characters)
- Username: Update your username (3-30 characters, alphanumeric + underscore)
- Bio: Update your bio (max 500 characters)
- Save: Click "Save Changes" to update your profile
- Name: Required, 2-50 characters
- Username: Required, 3-30 characters, only letters, numbers, and underscores
- Bio: Optional, max 500 characters
- Avatar: Optional, image files only, max 5MB
- Authentication: JWT token required for updates
- Authorization: Users can only update their own profiles
- Input Validation: Server-side validation for all inputs
- File Validation: Image type and size validation
- SQL Injection Protection: Mongoose ODM protection
- XSS Protection: Input sanitization
- Client-side Validation: Real-time validation feedback
- File Type Validation: Only image files allowed
- File Size Validation: 5MB limit enforced
- Error Handling: Secure error message display
- File Selection: User selects an image file
- Client Validation: Check file type and size
- Preview: Show image preview before upload
- Upload: Send to backend with FormData
- Cloudinary Processing: Upload to Cloudinary with optimization
- Old Avatar Cleanup: Delete previous avatar from Cloudinary
- Database Update: Save new avatar URL to database
- UI Update: Update profile display with new avatar
- Real-time Check: Backend checks for existing usernames
- Case Insensitive: Usernames are stored in lowercase
- Validation: Only alphanumeric characters and underscores allowed
- Error Handling: Clear error message if username is taken
- Smart Detection: Only allow save when actual changes are made
- Form State: Track original values vs current values
- File Changes: Detect when new avatar is selected
- User Feedback: Disable save button when no changes detected
- File Too Large: "File size must be less than 5MB"
- Invalid File Type: "Only image files are allowed"
- Username Taken: "Username is already taken"
- Validation Errors: Field-specific validation messages
- Network Errors: "Failed to update profile"
- Clear Messages: Specific error messages for each issue
- Retry Options: Users can retry failed operations
- Form Persistence: Form data is preserved on errors
- Graceful Degradation: App continues to work even with errors
- Image Optimization: Cloudinary automatically optimizes images
- File Size Limits: 5MB limit prevents large uploads
- Efficient Queries: Optimized database queries
- Error Logging: Comprehensive error logging for debugging
- Lazy Loading: Components load only when needed
- Image Preview: Client-side preview without server round-trip
- Debounced Validation: Efficient validation without excessive API calls
- Optimistic Updates: Immediate UI feedback
- Touch-Friendly: Large touch targets for mobile
- Responsive Layout: Adapts to all screen sizes
- Mobile Upload: Easy file selection on mobile devices
- Optimized Forms: Mobile-optimized form inputs
{
users: {
current: userObject,
status: "idle" | "loading" | "succeeded" | "failed",
error: null,
updateStatus: "idle" | "loading" | "succeeded" | "failed",
updateError: null
}
}fetchUser(id): Fetch user profileupdateProfile(data): Update profile without avatarupdateProfileWithAvatar(formData): Update profile with avatarclearUser(): Clear current userclearUpdateError(): Clear update errors
Your profile update and avatar upload system is now fully functional! Users can:
β
Update their profile information
β
Upload and change their avatar
β
See real-time validation feedback
β
Experience smooth, responsive UI
β
Have their data securely stored in Cloudinary
The system is production-ready with comprehensive error handling, validation, and security features.