Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on this project! I am approving your submission as you've successfully implemented all the required features. The code is clean, well-structured, and follows modern best practices for a Node.js application.
I have a couple of suggestions to make your code even more robust:
- In
src/controllers/auth.controller.ts, the emptycatchblock for token deletion can hide unexpected database errors. A safer approach is to useprisma.token.deleteMany({ where: { userId } })in your repository, which doesn't throw an error if no token is found, removing the need for atry...catch. - In your
migration.sqlfile, consider usingON DELETE CASCADEfor the foreign key in thetokenstable. This automatically cleans up a user's tokens if their account is ever deleted, which is great for database integrity.
These are not blockers but are good practices to keep in mind. Overall, this is a very strong submission. Well done!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| CREATE UNIQUE INDEX "tokens_user_id_key" ON "tokens"("user_id"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "tokens" ADD CONSTRAINT "tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
There was a problem hiding this comment.
Using ON DELETE RESTRICT prevents a user from being deleted if they have an associated token. While there's no user deletion feature in the current requirements, it's generally better practice to use ON DELETE CASCADE. This would automatically delete the user's token if the user record is ever deleted, making the database schema more robust and easier to manage in the future.
src/controllers/auth.controller.ts
Outdated
| try { | ||
| await tokensRepository.deleteByUserId(user.id); | ||
| } catch {} |
There was a problem hiding this comment.
An empty catch block can hide unexpected database errors, not just the "token not found" error you're likely trying to handle when a user logs in for the first time. A more explicit and safer approach would be to use a different Prisma method. Consider using deleteMany({ where: { userId: user.id } }), which doesn't throw an error if no matching token is found, removing the need for a try...catch block altogether.
src/controllers/auth.controller.ts
Outdated
| } | ||
|
|
||
| await usersRepository.activate(email); | ||
| await sendAuthentication(res, user); |
There was a problem hiding this comment.
The user object passed to sendAuthentication here is stale. It was fetched before the call to usersRepository.activate(), which modifies the user's state in the database. While it doesn't cause a bug with the current implementation, it's a good practice to use the most up-to-date data. The activate repository function returns the updated user, so you should capture it and pass it to sendAuthentication. For example:
const activatedUser = await usersRepository.activate(email);
await sendAuthentication(res, activatedUser);
No description provided.