Skip to content

Username Collision in OAuth Sign-in Process #3

@yntpdotme

Description

@yntpdotme

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:

  • Email addresses are guaranteed to be unique

  • Matches common platform conventions

    // auth.ts
    const username = account.provider === "github"
      ? (profile?.login as string)
      : user.email!.split('@')[0].trim(); // e.g., "akashkadlag" from "akashkadlag@gmail.com"

Potential Issue

  • A critical collision scenario exists between OAuth and Email-Password authentication:

    // Problematic scenario
    Email-Password User: username = "abcd"
    Google OAuth User: email = "abcd@gmail.com" -> username = "abcd" // Collision!

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions