Refactoring NestJS Auth Built with brkpt-auth: How Easy Is It, Really?
The last post built a password + Google sign-in MVP with brkpt-auth . This one makes two changes to that same project, the kind that come up naturally as a product grows, and checks what moved. Adding a unique username, without giving up email sign-in Say the product now needs a public, unique handle, something people can share and search for, that isn't tied to an email address they might change…
In this refactored NestJS auth system built with brkpt-auth, two key changes are made to accommodate a unique username field alongside email sign-in. This addition allows users to have a public, shareable handle that remains consistent even if their email changes later. The brkpt-auth framework initially used email as the identifier for convenience, but it now collects both email and username for sign-in through the updated DTOs.
To ensure the security of email addresses, the verify-email feature is implemented, which restricts access until the address is confirmed. However, this feature is not integrated in this example to maintain focus on the adapter changes. The Prisma schema is updated to include a unique username field, which requires a separate migration plan for existing users. In this case, the database is reset to simplify the focus on adapter changes rather than migration scripts.
The DTOs are adjusted to include username and email fields for both sign-up and sign-in. The findUserByDto method in the CredentialsAdapter is modified to accept either username or email as the sign-in identifier. It now checks both fields for conflicts during sign-up, preventing the creation of an account if either identifier is already taken. The mapUserToJwtPayload function in the JWT payload updates to include the username field, which can be displayed without an additional request.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.