Introduction and Overview

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.

Integrating Mongoose with NestJS for Todo Management
Creating the Todo Schema with User Association

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:

Explanation:
  • 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.
Updating the Todo Service to consider ToDo owners

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:

Explanation:
  • findAll and findOne: Fetches ToDos for the given userId. This method uses the ownerId to ensure only the user's ToDos are returned.
  • createTodo: This funciton ensures the ownerId 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.
Developing the Todo Controller

The TodoController handles incoming HTTP requests related to ToDo operations and communicates with the TodoService.

An example endpoint within our TodoController:

Explanation:
  • @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 the AuthGuard), 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 the ToDoService.
  • The controller has similar logic for other HTTP operations (POST, PUT, DELETE), consistently maintaining the association with users.
Example: Working with ToDos and Users

We can see this association in action through an example using send_request.ts:

Explanation:
  • 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.
Summary and Preparing for Practice

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal