Implement unique username generation based on email domain (#793) #794
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes Made:
Introduced a generateUsername function:
This function takes an email address, splits it into the local part and the domain part, and creates a unique username by appending a hashed version of the domain to the local part.
The hashing uses the SHA-256 algorithm and takes the first 8 characters of the hash to keep it concise.
Updated the registration controller:
Integrated the new generateUsername function to generate unique usernames before sending the registration request.
Why These Changes Were Made:
Problem: Previously, usernames were generated using only the local part of the email (e.g., [email protected] would generate xyz as the username). This led to conflicts when multiple users had the same local part but different domains (e.g., [email protected] and [email protected]).
Solution: By incorporating the domain part into the username (through hashing), we ensure that usernames are unique even if the local parts of the emails are the same but the domains are different.
Example:
Email: [email protected] -> Username: xyz_937d4a34
Email: [email protected] -> Username: xyz_85e5324f
Email: [email protected] -> Username: abc_937d4a34
(USING FIRST 8 PARTS OF HASHED )
This approach ensures that usernames are unique, reducing the chances of conflicts during user registration.