- 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.4 KiB
7.4 KiB
Clerk Configuration: Email Optional Setup
Overview
This guide explains how to configure Clerk to allow users to sign up and sign in without requiring an email address, while still supporting email as an optional field.
Step-by-Step Configuration
1. Access Clerk Dashboard
- Go to Clerk Dashboard
- Select your application
- Navigate to User & Authentication in the left sidebar
2. Configure Authentication Methods
A. Username Configuration
- Click on Email, Phone, Username in the left menu
- Find the Username section
- Toggle Username to ON
- Set Username as Required
- Configure username settings:
- Minimum length: 3 characters (recommended)
- Maximum length: 20 characters (recommended)
- Allow special characters: Optional (e.g., underscores, hyphens)
B. Email Configuration
- In the same Email, Phone, Username section
- Find the Email address section
- Toggle Email address to ON
- Set Email address as Optional (NOT required)
- Configure email settings:
- Email verification: Recommended (when email is provided)
- Allow disposable emails: Your choice
C. Phone Configuration (Optional)
- If you want to support phone authentication:
- Toggle Phone number to ON
- Set as Optional
- Configure SMS provider if needed
3. Configure Sign-Up Options
- Navigate to User & Authentication → Sign-up
- Under Required fields:
- ✅ Check Username (required)
- ❌ Uncheck Email address (optional)
- Under Optional fields:
- ✅ Check Email address
- ✅ Check First name (recommended)
- ✅ Check Last name (recommended)
4. Configure Sign-In Options
- Navigate to User & Authentication → Sign-in
- Under Identifiers:
- ✅ Enable Username
- ✅ Enable Email address (as optional)
- Set Username as the primary identifier
- Users can sign in with either username or email (if they provided one)
5. Configure Social Connections (Optional)
If you want to support social login:
- Navigate to User & Authentication → Social connections
- Enable providers (Google, Facebook, etc.)
- Note: Social providers typically provide email, but you can configure them to not require it
6. Session & Security Settings
- Navigate to User & Authentication → Sessions
- Configure session duration as needed
- Enable multi-session if desired
7. Customize Sign-Up/Sign-In UI
- Navigate to Customization → Components
- Customize the appearance of sign-up/sign-in forms
- Add custom text to explain that email is optional:
- "Email is optional but recommended for notifications"
- "You can add your email later in profile settings"
Testing the Configuration
Test Sign-Up Without Email
- Go to your application's sign-up page
- Try creating an account with:
- Username:
testuser123 - Password:
SecurePass123! - Leave email blank
- Username:
- Sign-up should succeed
Test Sign-In With Username
- Go to your application's sign-in page
- Sign in with:
- Username:
testuser123 - Password:
SecurePass123!
- Username:
- Sign-in should succeed
Test Adding Email Later
- Sign in to your application
- Navigate to user profile settings
- Add an email address
- Verify the email (if verification is enabled)
- Email notifications should now work
Backend Configuration
The backend has been updated to handle optional emails:
Database Schema
model User {
email String? @unique(map: "email") @db.VarChar(255)
// email is now nullable
}
User Creation
// Backend automatically handles missing email
email: clerkUser.emailAddresses[0]?.emailAddress || null
Notification Logic
// Email notifications are only sent to users with:
// 1. notificationEmail = true
// 2. email IS NOT NULL
// Users without email only receive in-app notifications
User Experience
For Users Without Email
- ✅ Can sign up with just username and password
- ✅ Can sign in with username
- ✅ Receive in-app notifications
- ❌ Cannot receive email notifications
- 💡 Prompted to add email for full notification support
For Users With Email
- ✅ Can sign up with username and email
- ✅ Can sign in with username or email
- ✅ Receive both in-app and email notifications
- ✅ Full feature access
Notification Behavior
In-App Notifications
- Always work for all users (with or without email)
- Stored in database
- Displayed in notification center
- Real-time updates
Email Notifications
- Only sent to users who:
- Have an email address in their profile
- Have
notificationEmailpreference enabled
- Gracefully skipped for users without email
- No errors or warnings for users without email
Profile Settings UI Recommendations
Add to your profile settings page:
// Show email status
{!user.email && (
<div className="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
<p className="text-sm text-yellow-800">
📧 Add an email address to receive email notifications
</p>
</div>
)}
// Email notification toggle
<label>
<input
type="checkbox"
checked={notificationEmail}
disabled={!user.email} // Disable if no email
/>
Email notifications
{!user.email && (
<span className="text-gray-500 text-sm ml-2">
(Add email first)
</span>
)}
</label>
Security Considerations
Account Recovery
Without email, users cannot:
- Reset password via email
- Receive account security alerts via email
Recommendations:
- Encourage users to add email for account security
- Consider adding phone number as backup
- Implement security questions as alternative
- Show warning during sign-up about account recovery
Rate Limiting
Without email verification:
- Implement stricter rate limiting on username sign-ups
- Monitor for abuse (multiple accounts from same IP)
- Consider CAPTCHA for sign-up
Username Validation
Enforce strong username rules:
- Minimum 3 characters
- No offensive words (use profanity filter)
- Unique across platform
- Cannot impersonate system accounts
Monitoring
Track these metrics:
- % of users signing up without email
- % of users adding email later
- Email notification delivery rate
- In-app notification engagement
Troubleshooting
Issue: Users can't sign up without email
Solution: Check Clerk Dashboard → Sign-up → Ensure email is NOT in "Required fields"
Issue: Email notifications failing
Solution: Check that:
- User has email in database
- User has
notificationEmail = true - Email service is configured correctly
Issue: Clerk components still require email
Solution:
- Clear browser cache
- Update
@clerk/clerk-reactto latest version - Verify Clerk Dashboard settings saved correctly
Migration for Existing Users
If you have existing users with temporary emails (@temp.com):
-- Find users with temporary emails
SELECT id, email, username FROM users WHERE email LIKE '%@temp.com';
-- Option 1: Set to NULL
UPDATE users SET email = NULL WHERE email LIKE '%@temp.com';
-- Option 2: Keep as-is (they can update in profile)
Support
For issues with Clerk configuration: