Search⌘ K

Implementing Sign-Up

Explore how to implement user sign-up securely in a NestJS application. Learn to validate user inputs with DTOs, handle errors like duplicate emails, and apply bcrypt hashing with salts to protect passwords during registration.

Having successfully set up all the essential user and authentication (auth) files, we’re ready to delve deeper. In this lesson, we will learn the sign-up process, ensuring users experience a seamless registration for our virtual library.

Sign-up form
Sign-up form

Defining SignUpDto

To register in our virtual library, users will need two essential pieces of information: email and password. To maintain the integrity of our system, we must ensure users provide both fields during the registration process.

Here are the requirements:

  • The value provided for email should be a valid email address.

  • The password should have a minimum length of 8 characters and a maximum length of 32 characters.

Based on these requirements, try to complete the SignUpDto:

Defining SignUpDto

Defining the signUp method in AuthService

The next step is to define the signUp method responsible for registration logic in AuthService.

Defining the signUp method in AuthService

Within the signUp method, we extract the email and password from SignUpDto and use the save method of this.userRepository to persist the user in our database.

If a user tries ...