Skip to main content
In this lesson, you’ll learn how to test authentication endpoints using Postman. Follow these steps to run the server, add authentication endpoints, and test them using Postman.

Starting the Server and Testing Additional Endpoints

Stop your current server with Ctrl+C. Then, open the “lesson two” index.js file and run the start command for lesson two. In this lesson, additional endpoints have been introduced. For example, consider the following DELETE endpoint for products:
Below is a sample of the server log output when running this command:

Adding User Authentication Endpoints

Now, add endpoints for user authentication, including signup and login functionalities. Scroll to the bottom of the “lesson two” index.js file to see an example of error handling:

Signup Endpoint

Add the following code for the signup endpoint:
When you run this endpoint, the server logs output similar SQL commands as before:
There are two authentication endpoints now:
  1. POST /signup: Creates a new user.
  2. POST /login: Authenticates an existing user by verifying the email and password.

Login Endpoint

Below is the code for the login endpoint:
The server log for the login endpoint execution is as follows:

Example JSON Responses

For demonstration purposes, here is an example JSON response when retrieving a product:

Testing Authentication Endpoints with Postman

Signup Request

To test the authentication process using Postman, create a new POST request for signup on localhost. Configure the request body as raw JSON similar to the sample below:
When you send the signup request, the API responds with user information similar to this:

Login Request

Next, create a POST request for login using the same email and password:
A successful login returns a success status and sets a cookie for the session:
After logging in, navigate to the cookie management section in Postman by clicking the “Cookies” button. This section displays session cookie details such as the name, domain, path, and value. Deleting the cookie simulates a logged-out state. For example, if you try to create a new product without a cookie, the API returns:
Once you log in again and obtain a new cookie, you can perform create, delete, or update operations on products. Note that retrieving a product list does not require authentication. An example response for fetching a product is:

Organizing Your Postman Collection

To keep your API requests organized:
  • Create a folder named products for all product-related requests.
  • Create another folder named auth for authentication endpoints.
  • You may leave miscellaneous test requests outside these folders if desired.
Below is an image of the Postman interface showing the structure of the ecommerce API requests for products:
The image shows a Postman interface with an "ecommerce" collection containing various API requests like "Get Products" and "Create Products." The authorization tab is open, displaying options for setting up request authentication.
After organizing your requests, resend the login request to verify the creation of the session cookie. Postman’s cookie management feature allows you to manually add, view, or delete cookies, which helps simulate different testing scenarios. For instance, if you delete the cookie, trying to create a product returns an “Unauthorized” error. Logging in again refreshes the cookie, and product operations can then proceed seamlessly. The following image illustrates the Postman interface with the authentication requests and the cookie details for a successful login:
The image shows a Postman interface with a collection of API requests related to "ecommerce" and "Auth," specifically highlighting a POST request to "localhost:4000/login." The response includes a cookie named "connect.sid" with details about its domain, path, and expiration.
With these configurations, your API now requires authentication (via a valid cookie) to create, update, or delete a product, while retrieving product lists remains open to all users.

Watch Video