This backend project implements a robust and secure user Authentication system using Node.js, designed for production environments. It provides comprehensive features including user registration, secure login with account lockout, password reset, two-factor authentication (2FA), and advanced token management.
- Secure User Registration:
- Passwords are securely hashed using
bcryptjs
.
- Passwords are securely hashed using
- Robust User Login:
- Implements account lockout after a configurable number of failed login attempts.
- Tracks failed login attempts to prevent brute-force attacks.
- Account lockout with automatic unlocking after a set period.
- Token-Based Authentication:
- Uses JWT (
jsonwebtoken
) for secure access and refresh tokens. - Refresh tokens are securely stored in HTTP-only cookies to mitigate XSS vulnerabilities.
/refresh-token
endpoint for seamless token renewal without re-login.
- Uses JWT (
- Secure Password Reset Tokens:
- Generates time-limited, cryptographically secure tokens.
- Email-Based Reset Links:
- Utilizes
Nodemailer
to send password reset links with secure tokens.
- Utilizes
- Token Verification & Password Update:
- Verifies reset tokens before allowing password changes.
- Single-use tokens with automatic expiry.
- TOTP Secret Generation & Storage:
- Generates and stores TOTP secrets using
speakeasy
.
- Generates and stores TOTP secrets using
- QR Code Setup:
- Generates QR codes (
qrcode
) for easy setup with authenticator apps.
- Generates QR codes (
- TOTP Verification at Login:
- Requires TOTP codes for enhanced login security.
- API Rate Limiting:
- Implements rate limiting using
express-rate-limit
to prevent brute-force attacks.
- Implements rate limiting using
- IP-Based Login Restrictions:
- Blocks repeated failed login attempts from specific IP addresses.
- Secure Logout:
- Clears refresh token cookies to ensure complete logout.
- Token Revocation:
- Invalidates tokens for enhanced security.
- Secure CORS setup:
- Enables Cross-Origin Resource Sharing in a secure manner using the cors package.
- Node.js: Backend runtime environment.
- Express: Web application framework.
- SQLite3: Database for user data and login attempt tracking.
- bcryptjs: Password hashing.
- jsonwebtoken: JWT token generation and verification.
- Nodemailer: Email sending for password resets.
- speakeasy: TOTP generation for 2FA.
- qrcode: QR code generation for 2FA setup.
- express-rate-limit: API rate limiting.
- cookie-parser: HTTP cookie management.
- cors: Cross-Origin Resource Sharing.
-
Clone the Repository:
git clone [repository-url] cd [project-directory]
-
Install Dependencies:
npm install
-
Database Setup:
- Ensure SQLite3 is properly configured.
- Run any database migrations or setup scripts if necessary.
-
Environment Configuration:
- Set up environment variables for JWT secrets, email credentials, and other sensitive information.
- Create a
.env
file and populate it with your environment variables. - Example
.env
file content:
PORT=5000 ACCESS_TOKEN_SECRET=your-access-token-key REFRESH_TOKEN_SECRET=your-refresh-token-key SERVICE_MAIL=[email protected] SERVICE_PASSWORD=your-email-password CLIENT_URL=http://yourwebsite.com
-
Start the Server:
node --env-file=.env server.js
-
API Documentation:
- Refer to the API documentation (if available) for endpoint details and usage.