A simple CRUD REST API to serve a Tasks Management App built using NodeJS in TypeScript.
This project is the backend for Tasks Management App built using NodeJS, TypeScript, and SQLite, incorporating Role-Based Access Control (RBAC) to manage user permissions effectively. The system provides a comprehensive RESTful API for user authentication, task management, and user management.
The authentication is utilizing JWT, a built-in NodeJS library known as Crypto for password hashing, and Zod for data validation.
- A. User Authentication
- Create New Account
- Login
- Logout
- Refresh Token
- B. Task Management by User
- Get All Tasks
- Get Task by ID
- Create New Task
- Edit Task
- Delete Task
- C. User Management by Admin
- Get All Users
- Get User by ID
- Archive User
- Delete User
- NodeJS: JavaScript runtime for building scalable network applications.
- TypeScript: A superset of JavaScript that compiles to plain JavaScript, providing static typing.
- SQLite: A lightweight database engine for local data storage. Supported in NodeJS as a built-in package since Node v22.5.0.
- Crypto: Node.js's built-in library for cryptographic functions, used for password hashing and verification.
- JWT (JSON Web Tokens): For secure user authentication and session management.
- Zod: A TypeScript-first schema declaration and validation library, used for validating user input and API requests.
To get started with this project, follow these steps:
-
Clone the repository
git clone https://github.com/ricoputrap/node-tasks-management-api cd node-tasks-management-api
-
Install Dependencies Make sure you have Node.js v22.5.0 or above and npm installed. Then run:
npm install
-
Set Up Environment Variables: Copy the
.env.example
file and rename it to.env
and fill in the values for all variables.-
How to generate values for
CRYPTO_KEY
andCRYPTO_IV
Write a simple JS script below:// generate-crypto-keys.js const crypto = require('crypto'); const key = crypto.randomBytes(32); const iv = crypto.randomBytes(16); const CRYPTO_KEY = key.toString('hex'); const CRYPTO_IV = iv.toString('hex'); console.log("CRYPTO_KEY:", CRYPTO_KEY) console.log("CRYPTO_IV:", CRYPTO_IV)
- Run the JS script above:
node generate-crypto-keys.js
- Store the generated value of
CRYPTO_KEY
andCRYPTO_IV
in your.env
file.
- Run the JS script above:
-
How to generate values for
ACCESS_TOKEN_SECRET
andREFRESH_TOKEN_SECRET
Basically you can put anything inside those two variables.
-
-
Compile TypeScript: Compile the TypeScript files to JavaScript:
npm run build
-
Run the Application: Start the server:
npm start
-
Testing the API: TODO
Path | Method | Description | Role |
---|---|---|---|
/api/auth/register |
POST | Create New Account | Public |
/api/auth/login |
POST | Login | User, Admin |
/api/auth/logout |
POST | Logout | User, Admin |
/api/auth/refresh-tokens |
POST | Refresh Token | User, Admin |
Path | Method | Description | Role |
---|---|---|---|
/api/tasks |
GET | Get All Tasks | User |
/api/tasks/:id |
GET | Get Task by ID | User |
/api/tasks |
POST | Create New Task | User |
/api/tasks/:id |
PUT | Edit Task | User |
/api/tasks/:id |
DELETE | Delete Task | User |
Path | Method | Description | Role |
---|---|---|---|
/api/users |
GET | Get All Users | Admin |
/api/users/:id |
GET | Get User by ID | Admin |
/api/users/:id |
DELETE | Delete User | Admin |
/api/users/:id?archive=1 |
DELETE | Archive User | Admin |