SecureAuth API is a robust, scalable, and production-ready authentication system built with Node.js, Express, MongoDB, and JWT. It implements industry-standard security practices including dual-token authentication, token rotation, session management, and OTP-based email verification.
Features โข Architecture โข Auth Flow โข API Endpoints โข Setup
| Feature | Description |
|---|---|
| ๐ JWT Dual-Token Auth | Short-lived Access Tokens + long-lived Refresh Tokens |
| ๐ Token Rotation | Refresh token rotated on every new access token request |
| ๐ฑ Session Management | Track & revoke sessions across all devices |
| ๐ง OTP Email Verification | Email verification via Nodemailer before account activation |
| ๐ Password Hashing | Cryptographic hashing using bcrypt |
| ๐ช Secure Cookies | HttpOnly, Secure cookies for refresh token storage |
| ๐ช Logout Everywhere | Invalidate all sessions with a single API call |
SecureAuth-API/
โ
โโโ config/
โ โโโ db.js # MongoDB connection via Mongoose
โ โโโ env.js # Environment variable management
โ
โโโ controllers/
โ โโโ authController.js # Registration, Login, Logout logic
โ โโโ otpController.js # OTP generation & verification
โ
โโโ models/
โ โโโ User.js # User schema & model
โ โโโ OTP.js # OTP schema & model
โ
โโโ routes/
โ โโโ authRoutes.js # API endpoint definitions
โ
โโโ utils/
โ โโโ mailer.js # Nodemailer email service
โ โโโ generateOTP.js # OTP utility function
โ
โโโ middleware/
โ โโโ authMiddleware.js # JWT verification middleware
โ
โโโ .env.example
โโโ server.js
โโโ package.json
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/register |
Register a new user | โ |
POST |
/login |
Login and receive tokens | โ |
POST |
/verify-otp |
Verify email via OTP | โ |
POST |
/refresh |
Rotate tokens (get new access token) | ๐ช Cookie |
POST |
/logout |
Logout from current device | โ |
POST |
/logout-all |
Logout from all devices | โ |
๐ฎ POST /api/auth/register
Request Body:
{
"username": "harshhere905",
"email": "harsh@gmail.com",
"password": "MySecurePass@123"
}Response 201:
{
"success": true,
"message": "User registered. Please verify email via OTP."
}Result:
๐ POST /api/auth/login
Request Body:
{
"email": "harsh@example.com",
"password": "MySecurePass@123"
}Response 200:
{
"success": true,
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "64f1a2b3c4d5e6f7a8b9c0d1",
"username": "harshhere905",
"email": "harsh@example.com"
}
}๐ช Refresh token is set as HttpOnly Secure Cookie automatically.
Result:
โ POST /api/auth/verify-otp
Request Body:
{
"email": "harsh@example.com",
"otp": "482910"
}Response 200:
{
"success": true,
"message": "Email verified. Account is now active."
}OTP Email Received:
API Result:
- Node.js v18+
- MongoDB Atlas account (or local MongoDB)
- Gmail or SMTP credentials for Nodemailer
git clone https://github.com/harshhere905/SecureAuth-API.git
cd SecureAuth-APInpm installcp .env.example .envEdit .env with your values:
# MongoDB
MONGO_URI=mongodb+srv://<user>:<password>@cluster.mongodb.net/secureauth
# JWT
JWT_SECRET=your_jwt_secret_key
JWT_REFRESH_TOKEN=your_refresh_token_secret
# Google OAuth
CLIENT_ID=your_google_client_id
CLIENT_SECRET=your_google_client_secret
# Email (Nodemailer)
EMAIL_USER=your_email@gmail.com# Development (with nodemon)
npm run dev
# Production
npm startServer runs at http://localhost:3000 ๐
- Passwords are hashed with
bcryptbefore storage โ never stored in plaintext - Refresh tokens are stored in
HttpOnly+Securecookies, inaccessible to JavaScript - Token Rotation โ refresh token is invalidated and replaced on every use to prevent replay attacks
- Session tracking on the server side enables instant revocation for all devices
- OTP expiry โ OTPs are time-limited and single-use
| Technology | Purpose |
|---|---|
| Node.js | Runtime environment |
| Express.js | Web framework & routing |
| MongoDB + Mongoose | Database & ODM |
| JSON Web Tokens (JWT) | Stateless authentication |
| bcrypt | Password hashing |
| Nodemailer | OTP email delivery |
| cookie-parser | Secure cookie handling |
| dotenv | Environment variable management |
| Variable | Description | Example |
|---|---|---|
MONGO_URI |
MongoDB connection string | mongodb+srv://... |
JWT_SECRET |
JWT access token secret | random_secret_key |
JWT_REFRESH_TOKEN |
JWT refresh token secret | another_secret_key |
CLIENT_ID |
Google OAuth client ID | xxxx.apps.googleusercontent.com |
CLIENT_SECRET |
Google OAuth client secret | GOCSPX-xxxx |
EMAIL_USER |
SMTP email address | you@gmail.com |
Contributions are welcome! Please open an issue first to discuss what you'd like to change.
git checkout -b feature/your-feature-name
git commit -m "feat: add your feature"
git push origin feature/your-feature-nameThis project is licensed under the MIT License โ see the LICENSE file for details.


