In this tutorial, you will learn how to build a secure, production-ready authentication REST API using Nuxt 3 (Nitro) as the backend.
We implement JWT authentication for stateless access control and add RSA encryption to securely encrypt user passwords on the client before sending them to the server. On the backend, passwords are decrypted using a private key and verified with bcrypt, ensuring strong security during login.
This step-by-step guide covers user registration, login, JWT-protected routes, profile access, logout, and Postman testing, all powered by a MySQL database. You’ll also learn essential security best practices for real-world applications.
📌 Overview
Build a secure REST API authentication system using:
Nuxt 3 backend (Nitro, no Express)
MySQL database
JWT for authentication tokens
RSA encryption to encrypt passwords on the client before sending to the backend
⚠️ JWT already secures authentication; RSA encrypts passwords in transit adding extra protection.
🧠 How Authentication Works
| Step | Description |
|---|---|
| Frontend | Encrypt password with RSA Public Key |
| Backend | Decrypt password with RSA Private Key |
| Verify decrypted password with bcrypt | |
| Issue JWT token on successful login | |
| Protect routes with JWT middleware |
✅ What This Tutorial Covers
Nuxt 3 backend setup (no Express)
MySQL integration with
mysql2RSA public/private key encryption helpers
Register & Login APIs
JWT-protected routes with middleware
Profile & Logout APIs
Postman testing
Production best practices
📦 Requirements
Node.js 18+
Nuxt 3
MySQL 8+
npm
OpenSSL (for key generation)
Postman (for API testing)
1️⃣ Create Nuxt 3 Project
2️⃣ Install Backend Dependencies
3️⃣ Create Project Structure
Create folders:
Create files:
4️⃣ Configure .gitignore
5️⃣ Environment Variables (.env)
6️⃣ Database Setup (MySQL)
7️⃣ Generate RSA Keys (OpenSSL)
8️⃣ MySQL Connection (server/db/mysql.js)
9️⃣ JWT Helper (server/utils/jwt.js)
🔟 RSA Decryption Helper (server/utils/rsa.js)
1️⃣1️⃣ Register API (server/api/auth/register.post.js)
1️⃣2️⃣ Login API (server/api/auth/login.post.js)
1️⃣3️⃣ JWT Middleware (server/middleware/auth.js)
1️⃣4️⃣ Profile API (server/api/auth/profile.get.js)
1️⃣5️⃣ Logout API (server/api/auth/logout.post.js)
▶️ Run the Application
Base URL:http://localhost:3000/api/auth
🚀 Postman Testing
| API | Method | Body / Headers | Description |
|---|---|---|---|
| Register | POST | { "name": "StarCode Kh", "email": "starcodekh@example.com", "password": "12345678" } | Creates a new user |
| Login | POST | { "email": "starcodekh@example.com", "password": "ENCRYPTED_PASSWORD_BASE64" } | Returns JWT token after decrypt+auth |
| Profile | GET | Header: Authorization: Bearer YOUR_JWT_TOKEN | Gets logged-in user profile |
| Logout | POST | Header: Authorization: Bearer YOUR_JWT_TOKEN | Handles logout (client-side) |
✅ Production Best Practices
Use HTTPS everywhere
Never commit
private.pemRotate RSA keys periodically
Use short-lived JWT tokens
Rate-limit login requests
Store secrets securely in
.env
🎯 Final Result
You now have a clean, standard, production-ready Nuxt 3 authentication API using:
🔐 RSA-encrypted login passwords
🔑 JWT authentication tokens
🛡 MySQL database backend
Download the complete Nuxt 3 JWT Authentication with RSA example from my GitHub repo here.

