- 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
3.4 KiB
3.4 KiB
Email-Optional Authentication Implementation
Overview
This document outlines the changes needed to make email optional for authentication while maintaining notification functionality.
Changes Required
1. Clerk Configuration (Dashboard)
In Clerk Dashboard (https://dashboard.clerk.com):
- Navigate to User & Authentication → Email, Phone, Username
- Configure authentication methods:
- Username: Enable and set as required
- Email: Enable but set as optional (not required)
- Phone: Enable as optional (if desired)
- Under Sign-up options:
- Set username as required field
- Make email optional
- Under Sign-in options:
- Enable "Username" as sign-in identifier
- Keep email as optional sign-in method
2. Database Schema Changes
File: server/prisma/schema.prisma
Make email field optional:
model User {
email String? @unique(map: "email") @db.VarChar(255)
// ... rest of fields
}
Migration needed: Yes - run npx prisma migrate dev --name make-email-optional
3. Backend Service Updates
A. User Service (server/src/services/userService.ts)
Update user creation to handle missing email:
email: clerkUser.emailAddresses[0]?.emailAddress || null,
B. Notification Service (server/src/services/notificationService.ts)
Update sendEmailNotifications to:
- Skip users without email addresses
- Log when email is skipped
- Only send in-app notifications for users without email
C. Email Service (server/src/services/emailService.ts)
Add validation to check if email exists before sending.
4. Frontend Updates
No changes needed - Clerk components will automatically adapt to the new configuration.
5. Notification Preference Logic
Users without email addresses:
notificationEmailpreference is ignored- Only
notificationInAppnotifications are sent - UI should show email field as optional in profile settings
- Display message: "Add an email address to receive email notifications"
Implementation Steps
- ✅ Update Clerk Dashboard settings
- ✅ Update database schema
- ✅ Run database migration
- ✅ Update backend services
- ✅ Test authentication flows
- ✅ Test notification delivery
Testing Checklist
- User can sign up with username only (no email)
- User can sign in with username
- User without email receives in-app notifications
- User without email does NOT receive email notifications (no errors)
- User can add email later in profile settings
- After adding email, user receives email notifications
- Existing users with email continue to work normally
Rollback Plan
If issues occur:
- Revert Clerk Dashboard settings (make email required)
- Run migration rollback:
npx prisma migrate resolve --rolled-back <migration_name> - Revert code changes
Security Considerations
- Username must be unique and validated
- Consider adding email verification when user adds email later
- Rate limiting on username-based sign-ups
- Monitor for abuse (fake accounts without email)
User Experience
For users without email:
- Clear messaging that email is optional
- Explanation of notification limitations
- Easy way to add email later
- In-app notifications work fully
For users with email:
- No changes to existing experience
- All notifications work as before