Node.
js Authentication with MySQL and Docker
This project is a robust authentication API developed using Node.js. It leverages
MySQL for database management and Docker for containerization. The API ensures
secure user authentication and is comprehensively documented with Swagger for ease
of use and integration.
Table of Contents
- Quick Links
- What You Need
- Key Features
- Project Layout
- Prerequisites
- Installation and Setup
- Using the App
- API Routes - Protected
- Credentials
Quick Links
- MySQL
- Docker
- OpenAPI 3.0
What You Need
- Node.js
- IDE
- Docker
- MySQL
Key Features
- User authentication bolstered with CORS support.
- Interactive API documentation facilitated by Swagger.
- Docker containerization for simplified deployment and scalability.
- Distinct environment configurations for development and production stages.
Project Layout
The project's directory structure is as follows:
- config/: Houses database configurations and other environment variables.
- controllers/: Contains control logic for authentication and protected routes.
- db/: Holds the database connection configuration.
- docs/: Contains API documentation in YAML format for Swagger.
- environment/: Contains environment variable files for different environments.
- routes/: Defines the API routes.
- services/: Contains services related to the user.
- Dockerfile and docker-compose.yml: Files necessary for Docker containerization.
Prerequisites
- Docker and Docker Compose (for containerization).
- Node.js (version 14.x or higher).
- MySQL (version 8.x or higher).
Installation and Setup
To start a new Node.js project, you can use the npm init command. The -y flag will
automatically fill in the default information in the setup process.
npm init -y
Navigate to the project directory:
cd Auth-Tutorial-Lab02
Install the necessary dependencies:
npm install
Using the App
Start the server with:
npm start
The server will run on http://localhost:3000/docs/
API Routes - Protected
Access to a protected resource requires basic authentication
http://localhost:3000/docs/Protected
Credentials
The project uses basic authentication to protect the /protected route. When making
an HTTP request to this route, you need to include these credentials in the
Authorization header of the request. The format is Authorization: Basic
{credentials}, where {credentials} is the Base64 encoding of the username and
password joined by a colon.
Use the following credentials for testing:
Username: basicUser
Password: basicPassword