Lightweight Node.js authentication API demonstrating JWT, refresh tokens, 2FA helpers, and simple file-based data storage.
This project implements authentication primitives (register, login, token refresh, logout) and user management endpoints. It uses Express and a small file-based datastore via nedb-promises. Utility modules provide token helpers, caching, and OTP/QR code support.
- JWT access tokens and refresh tokens
- Token invalidation and refresh handling
- Basic role/authorization middleware
- OTP (2FA) helper support and QR code generation
- Simple file-based storage (NeDB)
- Node.js + Express
- JSON Web Tokens (
jsonwebtoken) - Password hashing (
bcryptjs) - NeDB (
nedb-promises) for lightweight persistence - In-memory cache (
node-cache) - OTP support (
otplib) andqrcode - Environment config via
dotenv
- Node.js 18+ recommended
Clone the repo and install dependencies:
git clone <repo-url>
cd node-auth-api
npm installCreate a .env file in the project root. Common variables used by this project include:
PORT— port the server listens on (default:3000)JWT_SECRET— secret for signing access tokensREFRESH_TOKEN_SECRET— secret for refresh tokensOTP_ISSUER— (optional) issuer name used when generating OTP QR codes
Adjust these as needed for your deployment.
From package.json:
npm start— start server withnode server.jsnpm run dev— start server withnodemonfor developmentnpm test— placeholder test script
Start the server:
npm run devBy default the app entry is server.js (see main in package.json).
server.js,app.js— app/server bootstraproutes/— route definitions (auth.routes.js,users.routes.js,roles.routes.js)controllers/— request handlersmiddleware/—authenticate.js,authorize.jsmodels/— data models (NeDB-backed)utils/token.js— token helperscache/— caching utilities
See the route files in the routes/ folder for available endpoints and required request payloads. Typical endpoints you will find:
POST /auth/registerPOST /auth/loginPOST /auth/refreshPOST /auth/logoutGET /users(protected)
- This project uses a lightweight file-based datastore; for production, consider switching to a managed DB (Postgres/MongoDB).
- Secure your secrets and use HTTPS in production.
ISC
Open the controllers in controllers/ and routes in routes/ to adapt endpoints. See middleware/ for auth/authorization hooks.