Hi everyone,
I’m a junior backend developer working with Node.js.
While working on my app, I made a typo in process.env and noticed that a user was still being created even though the token was not generated successfully.
I understand why this happened technically, but from a business-logic perspective, this flow doesn’t feel correct. A user should not be created if their token generation fails.
After some research, I found that one possible solution is to use a database transaction to ensure that both operations creating the user and generating the token either succeed together or fail together.
This way, the signup process remains consistent and reliable.
My question is: Is using a transaction the best approach for this case, or are there other recommended patterns or solutions that I might be missing?
Thanks!