This tutorial shows how to build a secure REST API authentication system using Node.js, Express, MySQL, JWT, and RSA encryption for login passwords.
⚠️ JWT already secures authentication.
RSA is added here to encrypt passwords on the client before sending them to the backend, providing an extra layer of protection.
🧠 How It Works
Authentication Flow
Frontend
Backend
✅ What This Tutorial Covers
-
Node.js + Express authentication
-
MySQL database integration
-
RSA public / private key encryption
-
Secure Register & Login APIs
-
JWT-protected routes
-
Logout handling
-
Production-ready best practices
📦 Requirements
-
Node.js 18+
-
MySQL 8+
-
npm
-
OpenSSL enabled
-
Postman (for testing)
-
Basic JavaScript knowledge
📁 Create node-rsa-auth Project Structure
1️⃣ Node.js Project
Open the project in VS Code:
2️⃣ Install Dependencies
Update package.json:
3️⃣ Project Structure
4️⃣ Environment Configuration
Edit .env:
5️⃣ Database Setup (MySQL)
✔️ Make sure the database and table exist.
6️⃣ Generate RSA Keys (Login Encryption)
📁 RSA Key Structure
7️⃣ MySQL Connection
src/config/db.js
8️⃣ JWT Helper
src/config/jwt.js
9️⃣ Authentication Controller
src/controllers/authController.js
🔟 JWT Middleware
src/middleware/authMiddleware.js
1️⃣1️⃣ Routes
src/routes/authRoutes.js
1️⃣2️⃣ Server Setup
src/server.js
▶️ Run the Application
Base URL:
🚀 Postman Testing
Register
Login (RSA Encrypted)
Encrypt password using public.pem, then send:
Get Profile
Logout
🔁 Authentication Flow Summary
Register → Password hashed
Login → Password encrypted (RSA)
Backend → Decrypt + bcrypt verify
JWT → Issued
Protected routes → Bearer token
✅ Production Best Practices
-
Always use HTTPS
-
Never commit
private.pem -
Rotate RSA keys
-
Use short-lived JWT tokens
-
Rate-limit login requests
-
Store secrets in
.env
🎯 Final Result
You now have a clean, standard, production-ready Node.js authentication system using:
🔐 RSA-encrypted login passwords
🔑 JWT authentication
🛡 MySQL database
Want the full source code?
Download the complete Node.js JWT Authentication with RSA example on my GitHub repo here.

