A Node.js microservice for user authentication and session management using Express.js and PostgreSQL.
├── db/
│ └── init.sql # Database initialization script
├── src/
│ ├── config/
│ │ └── database.js # Database configuration
│ ├── controllers/
│ │ └── UserController.js # User-related business logic
│ ├── middleware/
│ │ └── auth.js # Authentication middleware
│ ├── models/
│ │ └── User.js # User model
│ ├── routes/
│ │ └── userRoutes.js # API routes definition
│ ├── app.js # Express application setup
│ └── index.js # Application entry point
├── docker-compose.yml # Docker services configuration
├── .env # Environment variables
└── package.json # Project dependencies and scripts
- Node.js: Runtime environment
- Express.js: Web framework
- PostgreSQL: Database
- Docker: Containerization
- JWT: Authentication tokens
- bcrypt: Password hashing
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(100)
);
CREATE TABLE sessions (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users (id),
token VARCHAR(100)
);
- POST /register: Register a new user
- POST /login: Authenticate user and get token
- GET /profile: Get user profile (requires authentication)
-
Clone the repository
-
Install dependencies:
npm install
-
Create a
.env
file with the following variables:PORT=your_port DB_USER=admin DB_PASSWORD=admin123456 DB_NAME=usersdb DB_HOST=localhost DB_PORT=5432 JWT_SECRET=your_jwt_secret
-
Start the PostgreSQL database:
docker-compose up -d
-
Run the application:
# Development mode npm run dev # Production mode npm start
- bcrypt: ^5.1.1 - Password hashing
- cors: ^2.8.5 - Enable CORS
- dotenv: ^16.4.7 - Environment variables
- express: ^4.21.2 - Web framework
- jsonwebtoken: ^9.0.2 - JWT authentication
- pg: ^8.13.1 - PostgreSQL client
- nodemon: ^3.1.9 - Development server with hot reload