This repository contains the backend for the User Authentication and Access Tracking System with Database Integration and Email Alerts project. Built with Express.js, the backend provides robust user authentication, multi-factor authentication (MFA), JWT-based session management, account lockout functionality, and email notifications. It integrates with PostgreSQL to manage user data, log login attempts, and enforce secure access control.
- Node.js (Express) – Server-side API development
- PostgreSQL – Persistent storage for user credentials, login activity, and blocklists
- JWT (JSON Web Tokens) – Session authentication using signed tokens
- bcryptjs – Password hashing and credential validation
- Redis – In-memory store for rate limiting and temporary blocks
- Nodemailer – Email service integration
- Live Backend Server: https://auth-access-tracker-be.onrender.com/
- Frontend: auth-access-tracker-fe
- Backend: auth-access-tracker-be
-
Sign-Up: Users can register new accounts with input validation. Passwords are hashed using bcrypt.
-
Sign-In: Credentials are validated using bcrypt. Upon successful verification, a JWT token is issued for session-based access.
-
Password Management:
- Forgot Password Flow: Tokenized reset link sent via email. The link directs users to a reset form and expires after 2 minutes.
- Password Update (While Logged In): Requires a valid JWT token and revalidation of the current password using bcrypt.
-
Authentication:
- JWT-based: Authenticates requests to protected routes (e.g., profile updates, account deletion).
- bcrypt-based: Used during login, password changes, and OTP verification.
PostgreSQL manages structured data for user accounts and activity logs. Key tables include:
- Users: Stores credentials, profile info, and MFA settings.
- Login Attempts: Logs attempts with timestamps, success flags, IPs, and fingerprints.
- Login History: Records successful logins with device/browser info.
- Blocked IPs: Tracks IPs temporarily blocked after repeated failures.
This schema follows a separation of concerns approach, simplifying auditing, scaling, and security maintenance.
This system uses layered safeguards to prevent unauthorized access:
- User-Based Lockout: Locks an account after 3 consecutive failed login attempts, and sends an alert email.
- IP-Based Blocking: Blocks all login attempts from the same IP after 7 consecutive failed attempts.
- Rate Limiting (Redis): Restricts 5 failed attempts within 8 seconds based on IP and device fingerprint.
⏱ In all three cases, the block duration is set to 30 seconds for development and debugging purposes.
- Uses fingerprinting and IP tracking to detect unfamiliar logins.
- Sends an email alert with metadata when a new browser/device logs in.
- Middleware ensures that JWT tokens are valid and active for accessing protected endpoints.
-
After login, users receive a 6-digit OTP via email.
- OTP is hashed using bcrypt and expires after 3 minutes.
- Validation requires a match and a valid expiration timestamp.
- Initiate Reset: Users submit their email via a public form.
- Tokenized Email Link: A hashed reset token is emailed and expires in 2 minutes.
- Anonymous Message: Generic success messages prevent disclosure of user existence.
- Reset Page: Users access a password update form via the link.
- Password Update: The token is verified, new password validated and hashed with bcrypt.
- Redirect: Successful resets redirect to the login page.
-
Clone the repository:
git clone https://github.com/your-username/auth-access-tracker-be.git cd auth-access-tracker-be -
Install dependencies:
npm install
-
Configure environment variables by creating a
.envfile:PORT=8899 PG_HOST=localhost PG_PORT=5432 PG_DATABASE=auth_access_tracker_dev PG_USER=postgres JWT_SECRET=your_jwt_secret EMAIL_USER=your_email@example.com EMAIL_PASS=your_email_password CLIENT_URL=http://localhost:5173 REDIS_URL=redis://... REDIS_PASSWORD=your_redis_password
🔑 To generate a JWT secret, run:
node generateJwtSecret.js
🔐 For Redis setup, create a Redis instance on Redis Cloud and retrieve credentials.
-
Set up the PostgreSQL database:
npm run db_schema npm run db_seed
-
Start the server:
npm start
Preliminary Testing Exposure: Basic unit tests using supertest were implemented to validate core routing logic and server responses.
npm testThis project is licensed under the MIT License. See the LICENSE file for more details.
For any inquiries or feedback, please contact: