Welcome to the final lesson of our course on implementing Authentication in our ToDo app with NestJS. In the previous lessons, we built a strong foundation by setting up user authentication using bcrypt
for password encryption, integrating JWT
for secure access, and protecting our endpoints with Guards
. In this lesson, we will focus on associating ToDo items with their respective users, which is crucial for maintaining data integrity and ensuring that users interact only with their own tasks.
The core of our task is to ensure each ToDo item is associated with a user. We'll achieve this by updating the Todo
schema to include an ownerId
property to signify which user it belongs to.
Here’s a breakdown of how we update the Todo
schema:
ownerId
: This field is critical as it links each ToDo item to a user, ensuring that users can only access their own ToDo items.title
,description
,completed
: These fields store the ToDo details, enabling users to manage their tasks' status.
The TodoService
contains the logic for CRUD operations on ToDo items, making sure these operations consider the user-specific association. We need to update all of them to require a userId
and relate ToDos with that user. This means that you can only list or update ToDos that you own. When you create a ToDo, it gets associated wit you.
Here’s a part of the TodoService
implementation:
findAll
andfindOne
: Fetches ToDos for the givenuserId
. This method uses theownerId
to ensure only the user's ToDos are returned.createTodo
: This funciton ensures theownerId
is added to the record in the database- The service contains other methods (
createTodo
,updateTodo
, etc.) that maintain this user association, ensuring that operations obey user-specific constraints.
The TodoController
handles incoming HTTP requests related to ToDo operations and communicates with the TodoService
.
An example endpoint within our TodoController
:
@Get()
: This endpoint fetches all ToDos for the current user. It uses a helper function,userIdFromRequest
, to extract the user identity from the request (which was set in theAuthGuard
), ensuring tasks are fetched only for the corresponding user.@Get(':id')
and@Post()
: These endpoints use the same mechanism to pass the user ID to theToDoService
.- The controller has similar logic for other HTTP operations (
POST
,PUT
,DELETE
), consistently maintaining the association with users.
We can see this association in action through an example using send_request.ts
:
createTodo
: Adds a new task for "User 1", associating the task with the user's identity through the token.getTodos
: Retrieves tasks, demonstrating user-specific filtering.
In this lesson, we explored how to associate ToDo items with users in a NestJS application, utilizing schemas, services, and controllers to maintain this link. By integrating Mongoose and leveraging its powerful schema capabilities, we ensured that each ToDo was managed securely and accurately.
As you move to the practice exercises, apply what you've learned by creating and managing your ToDos while maintaining these user associations. Congratulations on reaching the end of the course! Your understanding of secure and scalable application development with NestJS and MongoDB provides a solid foundation for future projects. Happy coding!
