This document describes the Referral Program implementation for the Authentication Flow in TeachLink. The referral program allows users to invite others to join the platform using unique referral codes, tracking referrals and providing benefits for both the referrer and the referred user.
- Referral Code Generation: Each user receives a unique 8-character referral code upon signup
- Referral Tracking: Users can enter a referral code during signup to track who referred them
- Referral Validation: The system validates referral codes before accepting them
- Referral Counting: Track the number of successful referrals for each user
- Self-Referral Prevention: Users cannot use their own referral code
Enhanced to support referral codes during user registration.
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"confirmPassword": "password123",
"referralCode": "ABCDEFGH" // Optional
}Response:
{
"message": "Account created successfully",
"user": {
"id": "user123",
"name": "John Doe",
"email": "john@example.com",
"referralCode": "NEWCODE1",
"referredBy": "ABCDEFGH",
"referralCount": 0,
"role": "STUDENT"
},
"token": "mock-jwt-token-123456"
}Validates a referral code before use during signup.
Query Parameters:
code(required): The referral code to validate
Response:
{
"valid": true,
"message": "Referral code is valid"
}Error Responses:
400: Invalid referral code format404: Referral code not found
The user schema has been extended to include referral-related fields:
{
id: string;
name: string;
email: string;
role: 'ADMIN' | 'INSTRUCTOR' | 'STUDENT' | 'GUEST';
referralCode?: string; // User's unique referral code
referredBy?: string; // Referral code used during signup
referralCount: number; // Number of users this user has referred
}- Length: 8 characters
- Character Set: A-Z (excluding I, O) and 2-9 (excluding 0, 1)
- Example:
ABCDEFGH,AB12CD34
The format excludes confusing characters (I, O, 0, 1) to improve readability and prevent user error.
The referral functionality is implemented in /src/lib/referral.ts with the following utilities:
generateReferralCode(): Generates a unique referral codevalidateReferralCode(code): Validates referral code formatreferralCodeExists(code): Checks if a referral code exists in the systemstoreReferralCode(email, code): Stores a referral code for a usergetReferralCodeOwner(code): Gets the owner of a referral codeincrementReferralCount(code): Increments the referral count for a codegetReferralCount(code): Gets the referral count for a code
The signup form now includes an optional referral code field:
<input type="text" placeholder="Enter referral code" {...register('referralCode')} />The field is optional and allows users to enter a referral code during registration.
- Code Validation: Referral codes are validated for format before checking existence
- Self-Referral Prevention: Users cannot use their own referral code
- Rate Limiting: Referral validation endpoints are rate-limited to prevent abuse
- Unique Codes: Codes are generated using a cryptographically secure random method
Unit tests for referral utilities are located in /src/lib/__tests__/referral.test.ts:
- Code generation uniqueness and format
- Format validation
- Storage and retrieval operations
- Referral count tracking
Integration tests for API endpoints are located in /src/app/api/referral/__tests__/validate.test.ts:
- Referral validation endpoint behavior
- Error handling for invalid codes
- Rate limiting compliance
E2E tests for the referral flow are in /e2e/auth/signup.spec.ts:
- Signup with valid referral code
- Signup without referral code
- Error handling for invalid referral codes
- Referral code field visibility
Potential future improvements to the referral program:
- Reward System: Implement actual rewards for successful referrals
- Referral Dashboard: Create a dashboard for users to track their referrals
- Referral Sharing: Add social media sharing buttons for referral codes
- Multi-level Referrals: Support multi-level referral programs
- Analytics: Provide analytics on referral performance
- Email Notifications: Send notifications when referrals are successful
- Referral Expiration: Add expiration dates to referral codes
- Bulk Referral Imports: Allow importing referral codes in bulk
When migrating from a system without referral support:
- Existing users will be assigned a referral code on their next login/update
- The
referralCodefield is optional and nullable for backward compatibility - The
referralCountdefaults to 0 for existing users - The
referredByfield is optional and nullable
- Referral code validation is fast (O(1) lookup in mock storage)
- In production, use database indexing on referral codes for optimal performance
- Consider caching referral code validation results for frequently used codes
- Implement batch processing for referral count updates if needed
- Referral codes follow accessibility best practices (no confusing characters)
- Referral program is optional and does not affect core functionality
- Users can opt-out of the referral program if desired
- Referral data is handled according to privacy policies and regulations
For issues or questions related to the referral program:
- Check the unit tests for usage examples
- Review the API endpoint documentation
- Contact the development team for complex scenarios
- Monitor referral validation logs for potential abuse patterns
- Initial implementation of referral program
- Referral code generation and validation
- Integration with signup flow
- Unit, integration, and E2E tests
- Documentation
Last Updated: 2025-05-30 Maintained By: TeachLink Development Team