Current Behaviour:
When users sign in with Google OAuth, the system creates a username by converting their display name to lowercase.
Since the username field in our database has a unique constraint, this causes issues
- Multiple users have the same display name
- New users with the same display name cannot sign in
Root Cause
- Google OAuth returns display names which aren't guaranteed to be unique
- Our current implementation doesn't handle username collisions
- Database unique constraint fails for duplicate usernames
Solution 1
We can use email prefix as username for Google OAuth users since:
Potential Issue
Solution 2
Add OAuth Provider Prefix
// auth.ts
const username = account.provider === "github"
? (`github_${profile?.login}` as string)
: (`google_${user.email!.split('@')[0]}` as string) // e.g google_abcd
P.S.: Are there better approaches to handle this username collision issue while maintaining uniqueness and usability? Would love to hear your thoughts!
Current Behaviour:
When users sign in with Google OAuth, the system creates a username by converting their display name to lowercase.
Since the username field in our database has a unique constraint, this causes issues
Root Cause
Solution 1
We can use email prefix as username for Google OAuth users since:
Email addresses are guaranteed to be unique
Matches common platform conventions
Potential Issue
A critical collision scenario exists between OAuth and Email-Password authentication:
Solution 2
Add OAuth Provider Prefix
P.S.: Are there better approaches to handle this username collision issue while maintaining uniqueness and usability? Would love to hear your thoughts!