This is a robust authentication microservice built with Node.js, Express, MongoDB, and Redis. It handles user registration, login, secure token management (using JWT and Refresh Tokens), and password recovery updates.
- User Registration & Login
- JWT Authentication (Access & Refresh tokens)
- Token Management (Refresh, Logout, Logout All Devices)
- Password Recovery (Forgot/Reset Password)
- Redis Integration for session/token management
- Security (Helmet, CORS, Cookie Parser)
-
Clone the repository:
git clone <repository-url> cd NodeJS/auth
-
Install dependencies:
npm install
-
Configure Environment Variables: Create a
.envfile in the root directory and add the following:PORT=3000 MONGODB_URI=mongodb://localhost:27017/your_db_name # Redis Configuration REDIS_URL=redis://localhost:6379 # OR use individual host/port # REDIS_HOST=localhost # REDIS_PORT=6379 # REDIS_PASSWORD=your_redis_password # JWT Secrets JWT_SECRET=your_super_secret_jwt_key # Email Service (for password reset) EMAIL_USER=your_email@example.com EMAIL_PASS=your_email_password # Frontend URLs (for email links) FRONTEND_URL=http://localhost:5173 FRONTEND_VERIFY_EMAIL_URL=http://localhost:5173/verify-email FRONTEND_FORGOT_PASS_URL=http://localhost:5173/reset-password
Start the development server:
node index.js
# OR if you have nodemon installed
npx nodemon index.jsThe server will run on http://localhost:3000 (or your defined PORT).
Base URL: /api/auth
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/register |
Register a new user account | No |
POST |
/login |
Log in and receive access/refresh tokens | No |
POST |
/refresh |
Refresh an expired access token | No |
POST |
/logout |
Log out current session (invalidates token) | Yes |
POST |
/logout-all |
Log out from all devices | Yes |
POST |
/forgot-password |
Request a password reset email | No |
POST |
/reset-password |
Reset password using token | No |
For routes requiring authentication (like logout), you must include the access token in the headers or cookies (depending on your specific extractToken middleware implementation, typically headers).
Authorization: Bearer <your_access_token>.
├── auth/
│ ├── auth.controller.js # Request handlers
│ ├── auth.routes.js # Route definitions
│ ├── models/ # Mongoose schemas
│ ├── repositories/ # Data access layer
│ └── services/ # Business logic
├── config/
│ ├── env.js # Environment config loader
│ └── ...
├── middleware/ # Custom middleware (Token extraction, etc.)
├── utils/ # Helper functions
├── index.js # Application entry point
├── package.json
├── .env
└── README.md