Adding Users to the Application

Learn how to add users to the application, and add some business logic to the application.

Register users

In this lesson, we’ll add the concept of users to the application. More routes will be created, as well as a whole new module and some business logic to handle users. Now, let’s add some users to the application!

We currently have the first endpoint running and listing all the museums in the application, but we’re still far from meeting the final requirements.

We want to add users so that it’s possible to register, log in, and interact with the application with an identity.

We’ll start by creating the object that will define the user, and then proceed into the business logic to create and store it. After this, we’ll create endpoints that will allow us to interact with the application via HTTP, allowing users to register.

Creating the user module

We currently have what we can call a single module in the application: the museums module. Everything that’s related to museums is there, from controllers to repositories, object definitions, and so on. This module has one single interface, which is its index.ts file.

We did this so that we have the freedom to work inside the module while maintaining its external API so that it’s always stable. This gives us a nice degree of decoupling between modules. To make sure that the pieces inside a module are reasonably decoupled, we must also inject their dependencies via a constructor, allowing us to easily swap pieces and test them in isolation.

Following those guidelines, we’ll keep using this modules system and create one for our users by following these steps:

  1. Create a folder named src/users, and put the index.ts file inside it.
  2. Create the src/users/types.ts file. This is where we’ll define the User type:

Get hands-on with 1200+ tech skills courses.