Table of Contents
This project creates basic REST API to manage users implemented in NodeJS and documented with Swagger that uses JWT authentication.
To simplify the project, the implementation has been done from the server side without using any database.
A JSON file is used to save data and mock up the database layer, although this logic can be transferred to other databases.
This project uses a 3 layer architecture:
- 1. Controller: Handles the requests and responses for our endpoints.
- 2. Service Layer: Defines business logic and exports the methods that are used by the controller.
- 3. Data Access Layer: This is the layer that interacts with the Database or in our case the local JSON file that mimics our Database.
npm must be installed.
To download the latest version of npm, on the command line, run the following command:
npm install -g npm@latest
To install the project we have to install npm and execute the following commands:
npm install
npm start
-
Swagger Web:
http://localhost:3000/api/docs
-
JSON:
http://localhost:3000/api/docs.json
The authorization is based in OAuth 2.0.
The access token is required to access to users secured enpoints.
The refresh token exists to enable authorization servers to use short lifetimes for access tokens without needing to involve the user when the token expires.
The requests can be performed to the following urls:
- POST
http://localhost:3000/api/v1/auth/token
- POST
http://localhost:3000/api/v1/auth/refresh
The requests can be performed to the following urls:
- GET
http://localhost:3000/api/users
- GET
http://localhost:3000/api/users/:userId
- POST
http://localhost:3000/api/users
- PUT
http://localhost:3000/api/users/:userId
- DELETE
http://localhost:3000/api/users/:userId
Use endpoint POST api/v1/auth/token. You can use this test user to authorize:
- email:
bettysloan@email.com
- password:
password1
A JWT will be issued for the test user specified:
Copy the value of access_token in Authorization to allow Swagger to send the Authorization header required in the secured enpoints:
As we can see the authorization header is sent in the request and we can access to the secure endpoint: