An open API service indexing awesome lists of open source software.

https://github.com/cristiancosta/node-ts-jwt

JWT authentication service made with Typescript, Node.js, Express and more.
https://github.com/cristiancosta/node-ts-jwt

bcryptjs eslint express husky jest jwt mysql nodejs prettier supertest swagger testcontainers typeorm typescript

Last synced: 5 months ago
JSON representation

JWT authentication service made with Typescript, Node.js, Express and more.

Awesome Lists containing this project

README

          

# Node TS JWT

![Test Status](https://img.shields.io/badge/Jest-tested-brightgreen?logo=jest)
![Build Status](https://img.shields.io/badge/build-passing-success?logo=github)

## ๐Ÿงฐ Stack

[![TypeScript](https://img.shields.io/badge/language-TypeScript-3178c6?logo=typescript)](https://www.typescriptlang.org/)
[![Node.js](https://img.shields.io/badge/Node.js-18%2B-green?logo=node.js)](https://nodejs.org/)
[![Express](https://img.shields.io/badge/Express.js-5.x-black?logo=express)](https://expressjs.com/)
[![JWT](https://img.shields.io/badge/JWT-secure-blue?logo=jsonwebtokens)](https://jwt.io/)
[![TypeORM](https://img.shields.io/badge/TypeORM-ORM-red?logo=typeorm)](https://typeorm.io/)
[![MySQL](https://img.shields.io/badge/MySQL-DB-4479A1?logo=mysql)](https://www.mysql.com/)
[![Swagger](https://img.shields.io/badge/Swagger-UI-brightgreen?logo=swagger)](https://swagger.io/)
[![Jest](https://img.shields.io/badge/Tested%20with-Jest-99425b?logo=jest)](https://jestjs.io/)
[![Testcontainers](https://img.shields.io/badge/Testcontainers-Integration--Testing-green?logo=docker)](https://testcontainers.com/)
[![Husky](https://img.shields.io/badge/Husky-Git%20Hooks-8e44ad?logo=git)](https://typicode.github.io/husky/)
[![Supertest](https://img.shields.io/badge/Supertest-API%20Testing-blueviolet)](https://github.com/visionmedia/supertest)
[![Prettier](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg?logo=prettier)](https://prettier.io/)
[![ESLint](https://img.shields.io/badge/linting-eslint-yellow?logo=eslint)](https://eslint.org/)

---

## ๐Ÿ” Introduction

This is a REST API built with **TypeScript**, **Node.js** and **Express** that implements secure authentication using **JSON Web Tokens (JWT)**. It's a great base for systems that require login, user management, and access control via tokens.

Features include:

- ๐Ÿ”’ Authentication with **Access Token** and **Refresh Token**.
- ๐Ÿ“– Interactive API documentation via **Swagger UI**.
- ๐Ÿงช Real **MySQL** integration testing using **Testcontainers**.
- โœจ **ESLint** + **Prettier** + **Husky** for code quality enforcement.
- ๐Ÿ” **GitHub Actions** for automated CI/CD.

---

## ๐Ÿ“ Project Structure

```
โ”œโ”€โ”€ .github/
โ”œโ”€โ”€ .husky/
โ”œโ”€โ”€ .vscode/ # Debuggers and workspace configuration.
โ”œโ”€โ”€ src/
| โ”œโ”€โ”€ controllers/ # Endpoint logic.
| โ”œโ”€โ”€ errors/ # Custom error classes.
| โ”œโ”€โ”€ middlewares/ # Auth, error handling, swagger auth.
| โ”œโ”€โ”€ models/ # Typeorm models.
| โ”œโ”€โ”€ repositories/ # Data access abstraction.
| โ”œโ”€โ”€ routes/ # Route definitions.
| โ”œโ”€โ”€ services/ # Business logic.
| โ”œโ”€โ”€ types/ # Data structure definitions.
| โ”œโ”€โ”€ utils/ # Reusable functions.
| โ”œโ”€โ”€ app.ts # Express app configuration.
| โ”œโ”€โ”€ configuration.ts # .env configuration entry point.
| โ”œโ”€โ”€ data-source.ts # Typeorm configuration.
| โ”œโ”€โ”€ server.ts # Entry point.
| โ””โ”€โ”€ swagger.ts # Swagger configuration.
โ”œโ”€โ”€ test/ # Unit and integration tests using jest, supertest and testcontainers.
โ”œโ”€โ”€ .gitignore # Default gitignore file provided by GitHub.
โ”œโ”€โ”€ .prettierrc # Prettier configuration file.
โ”œโ”€โ”€ eslint.config.mjs # ESLint configuration file.
โ”œโ”€โ”€ jest.config.ts # Jest configuration file.
โ”œโ”€โ”€ LICENSE # MIT License.
โ”œโ”€โ”€ package-lock.json # Exact project dependencies tree.
โ”œโ”€โ”€ package.json # Project dependencies, scripts and more stuff.
โ”œโ”€โ”€ README.md # Current file.
โ”œโ”€โ”€ tsconfig.eslint.json # ESLint typescript config file.
โ””โ”€โ”€ tsconfig.json # Project typescript config.
```

---

## ๐Ÿš€ Quick Installation

```bash
git clone https://github.com/cristiancosta/node-ts-jwt.git
cd node-ts-jwt
npm install
```

Then create a `.env` file in the project root with the following content:

```env
DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=root
DB_NAME=nodejwt
DB_PORT=3306

SERVER_PORT=8081

JWT_SECRET=mysecret
JWT_ACCESS_TOKEN_DURATION=2 hours
JWT_REFRESH_TOKEN_DURATION=2 days

SWAGGER_USERNAME=admin
SWAGGER_PASSWORD=admin
```

Make sure you have a MySQL database up and running. In my case, I use Docker since it's the easiest way:

```bash
$ docker run -p 3306:3306 -e MYSQL_ROOT_USER=root -e MYSQL_ROOT_PASSWORD=root -d mysql
```

Log into the container and create the database:

```bash
$ docker exec -it mysql -uroot -proot
$ CREATE DATABASE nodejwt;
```

Start the server:

```bash
npm start
```

๐Ÿ“ The API will be available at: `http://localhost:8081`

---

## ๐Ÿ“š Interactive Documentation

You can explore and test all endpoints using Swagger UI:

๐Ÿ”— [http://localhost:8081/api-docs/](http://localhost:8081/api-docs/)

Basic Auth required:

- **Username:** `admin`
- **Password:** `admin`

You can change Swagger credentials on `.env` file.

---

## ๐Ÿงช Testing with Testcontainers

This project uses **Testcontainers** to spin up a real MySQL instance during tests. This ensures:

- A test environment **identical to production**.
- No fragile mocks.
- Automatic container cleanup.

Run tests:

```bash
npm test
```

---

## โœ… Husky + Pre-commit hooks

The project uses **Husky** to automatically run the following before each commit:

```bash
npx lint-staged # Runs ESLint + Prettier
```

---

## โš™๏ธ GitHub Actions CI

The project runs automated tests and perform building process on Node.js 20.x and 22.x via GitHub Actions.

---

## ๐Ÿ› ๏ธ Useful Scripts

| Command | Description |
| ------------------ | ------------------------------------------------------------- |
| `npm start` | Starts the server with Nodemon |
| `npm test` | Runs all tests using Jest + Testcontainers |
| `npm run lint` | Lints the code using ESLint |
| `npm run lint:fix` | Lints and auto-fixes issues |
| `npm run format` | Formats code with Prettier |
| `npm run build` | Builds the project with tsc |
| `npm run serve` | Starts the server with Node (previous build script execution) |

---

## ๐Ÿค Contributing

Contributions are welcome!

1. Fork the repo.
2. Create a new branch.
3. Submit a Pull Request.
4. Make sure lint and tests pass.

๐Ÿ“ฉ For direct contact: **cristiancosta1991@gmail.com**
๐ŸŒŸ Found it useful? Give the project a โญ on GitHub!

[https://github.com/cristiancosta/node-ts-jwt](https://github.com/cristiancosta/node-ts-jwt)

---

## โ˜• Donations

If you'd like to support this project, feel free to donate a coffee.

[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/C0C81GYN0D)

---

## ๐Ÿ“ License

MIT ยฉ [Cristian Costa](mailto:cristiancosta1991@gmail.com)