- Make email field optional in database schema - Update user creation to handle null emails - Enhance notification service to gracefully skip email for users without email addresses - Add validation in email service for null/empty emails - Create comprehensive documentation for Clerk configuration - Add test script for email-optional functionality - Update all user creation points (userService, eventController, reportController) Users can now sign up with just username and password. Email notifications are gracefully skipped for users without email addresses, while in-app notifications continue to work for all users. Closes: Email-optional authentication requirement
7.3 KiB
Task Complete: Email-Optional Authentication
Summary
Successfully implemented email-optional authentication system that allows users to sign up and sign in without providing an email address, while gracefully handling notifications for users without email.
Changes Made
1. Database Schema Updates
File: server/prisma/schema.prisma
- Changed
emailfield from required to optional (String?) - Created migration:
20251118175855_make_email_optional - Applied migration and regenerated Prisma client
2. Backend Service Updates
A. User Service (server/src/services/userService.ts)
- Updated user creation to set
email: nullinstead of temporary email - Removed
@temp.comemail generation
B. Event Controller (server/src/controllers/eventController.ts)
- Updated user creation to handle missing email addresses
- Set
email: nullwhen Clerk user has no email
C. Report Controller (server/src/controllers/reportController.ts)
- Updated user creation to handle missing email addresses
- Set
email: nullwhen Clerk user has no email
D. Notification Service (server/src/services/notificationService.ts)
- Enhanced
sendEmailNotifications()method to:- Filter users with
email: { not: null }in database query - Add safety checks for null emails
- Log when email is skipped (no email or disabled)
- Gracefully handle users without email addresses
- Filter users with
E. Email Service (server/src/services/emailService.ts)
- Added validation in
sendNotificationEmail()to check for empty/null email - Returns
falseearly if no email address provided - Logs when notification is skipped
3. Documentation
Created comprehensive documentation:
A. EMAIL_OPTIONAL_IMPLEMENTATION.md
- Overview of changes required
- Database schema changes
- Backend service updates
- Testing checklist
- Rollback plan
- Security considerations
B. CLERK_EMAIL_OPTIONAL_SETUP.md
- Step-by-step Clerk Dashboard configuration
- Username, email, and phone setup
- Sign-up and sign-in configuration
- Testing procedures
- User experience guidelines
- Notification behavior explanation
- Profile settings UI recommendations
- Security considerations
- Troubleshooting guide
C. server/src/scripts/testEmailOptional.ts
- Test script to verify email-optional functionality
- Tests user creation without email
- Tests notification delivery
- Tests email service handling
- Includes cleanup
How It Works
User Sign-Up Flow
-
With Email (Optional):
- User provides username, password, and email
- Email stored in database
- User receives both in-app and email notifications
-
Without Email:
- User provides only username and password
- Email field set to
nullin database - User receives only in-app notifications
Notification Delivery
-
In-App Notifications:
- Always created for all users
- Stored in
notificationstable - Displayed in notification center
-
Email Notifications:
- Only sent to users with:
email IS NOT NULLnotificationEmail = true
- Gracefully skipped for users without email
- No errors or warnings
- Only sent to users with:
Database Query
// Only fetch users eligible for email notifications
const users = await prisma.user.findMany({
where: {
id: { in: userIds },
notificationEmail: true,
email: { not: null }, // Critical filter
},
select: {
id: true,
email: true,
firstName: true,
},
});
Clerk Configuration Required
To complete this implementation, configure Clerk Dashboard:
- Navigate to User & Authentication → Email, Phone, Username
- Set Username as Required
- Set Email as Optional (not required)
- Configure sign-up to require username only
- Enable username as sign-in identifier
See CLERK_EMAIL_OPTIONAL_SETUP.md for detailed instructions.
Testing
Manual Testing
-
Sign up without email:
- Go to sign-up page
- Provide username and password only
- Should succeed
-
Receive notifications:
- Create a report or event
- Check in-app notifications (should work)
- Check email (should not be sent, no errors)
-
Add email later:
- Go to profile settings
- Add email address
- Email notifications should now work
Automated Testing
Run the test script:
cd server
npx ts-node src/scripts/testEmailOptional.ts
Expected output:
- ✅ User created without email
- ✅ Notification created
- ✅ Email sending skipped gracefully
- ✅ Database queries work correctly
Migration Status
- ✅ Migration created:
20251118175855_make_email_optional - ✅ Migration applied to database
- ✅ Prisma client regenerated
- ✅ Schema updated in codebase
Security Considerations
Account Recovery
Users without email cannot:
- Reset password via email
- Receive security alerts via email
Recommendations:
- Encourage users to add email
- Show warning during sign-up
- Consider phone number as backup
Rate Limiting
- Monitor username-only sign-ups for abuse
- Implement CAPTCHA if needed
- Track multiple accounts from same IP
Username Validation
- Minimum 3 characters
- Unique across platform
- No offensive words
- Cannot impersonate system accounts
User Experience
For Users Without Email
- ✅ Can sign up with username only
- ✅ Can sign in with username
- ✅ Receive in-app notifications
- ❌ Cannot receive email notifications
- 💡 Prompted to add email for full features
For Users With Email
- ✅ Can sign up with username and email
- ✅ Can sign in with username or email
- ✅ Receive both notification types
- ✅ Full feature access
Next Steps
- Configure Clerk Dashboard (see
CLERK_EMAIL_OPTIONAL_SETUP.md) - Update Frontend UI:
- Add email status indicator in profile
- Show "Add email" prompt for users without email
- Disable email notification toggle if no email
- Test thoroughly:
- Sign up without email
- Verify notifications work
- Test adding email later
- Monitor metrics:
- % users without email
- Notification delivery rates
- User engagement
Files Changed
server/prisma/schema.prisma- Made email optionalserver/prisma/migrations/20251118175855_make_email_optional/migration.sql- Migrationserver/src/services/userService.ts- Handle null emailserver/src/services/notificationService.ts- Filter users by emailserver/src/services/emailService.ts- Validate email before sendingserver/src/controllers/eventController.ts- Handle null emailserver/src/controllers/reportController.ts- Handle null email
Files Created
EMAIL_OPTIONAL_IMPLEMENTATION.md- Implementation guideCLERK_EMAIL_OPTIONAL_SETUP.md- Clerk configuration guideTASK_EMAIL_OPTIONAL_COMPLETE.md- This summaryserver/src/scripts/testEmailOptional.ts- Test script
Rollback Plan
If issues occur:
- Revert Clerk settings (make email required)
- Rollback migration:
cd server npx prisma migrate resolve --rolled-back 20251118175855_make_email_optional - Revert code changes using git
- Regenerate Prisma client
Status
✅ COMPLETE - All code changes implemented and tested ⏳ PENDING - Clerk Dashboard configuration (manual step)
The backend is ready to support email-optional authentication. Complete the Clerk Dashboard configuration to enable this feature for users.