Automated Action 84cb69bf10 Rewrite authentication service from FastAPI/Python to Node.js/Express
- Replace FastAPI with Express.js framework
- Replace SQLAlchemy with Sequelize ORM
- Replace Alembic with Sequelize migrations
- Implement bcryptjs for password hashing
- Add JWT authentication with jsonwebtoken
- Create Express routes and controllers
- Add input validation with express-validator
- Implement rate limiting and security headers
- Configure CORS for all origins
- Add environment-based configuration
- Update README with Node.js setup instructions

Environment variables required:
- JWT_SECRET: JWT secret key for token signing
- NODE_ENV: Environment (development/production)
- PORT: Server port (default: 3000)
- JWT_EXPIRES_IN: Token expiration time (default: 24h)
2025-06-27 09:28:13 +00:00

2.9 KiB

User Authentication Service

A Node.js Express-based user authentication service with JWT token authentication and SQLite database.

Features

  • User registration and login
  • JWT token-based authentication
  • Password hashing with bcryptjs
  • SQLite database with Sequelize ORM
  • Input validation with express-validator
  • Rate limiting and security headers
  • CORS enabled for all origins
  • Health check endpoint
  • Environment-based configuration

Environment Variables

Create a .env file in the root directory with the following variables:

  • NODE_ENV: Environment (development/production)
  • PORT: Server port (default: 3000)
  • JWT_SECRET: JWT secret key for token signing (required for production)
  • JWT_EXPIRES_IN: Token expiration time (default: 24h)

Copy .env.example to .env and update the values:

cp .env.example .env

Installation

  1. Install Node.js dependencies:
npm install
  1. Set up environment variables:
cp .env.example .env
  1. Start the application in development mode:
npm run dev

Or start in production mode:

npm start

API Endpoints

Public Endpoints

  • GET / - Service information
  • GET /health - Health check
  • POST /api/v1/auth/register - User registration
  • POST /api/v1/auth/login - User login

Protected Endpoints (require Bearer token)

  • GET /api/v1/users/me - Get current user info
  • GET /api/v1/users/profile - Get user profile
  • PUT /api/v1/users/profile - Update user profile
  • DELETE /api/v1/users/deactivate - Deactivate user account

Usage Examples

  1. Register a new user:
curl -X POST "http://localhost:3000/api/v1/auth/register" \
     -H "Content-Type: application/json" \
     -d '{"email": "user@example.com", "password": "password123"}'
  1. Login to get access token:
curl -X POST "http://localhost:3000/api/v1/auth/login" \
     -H "Content-Type: application/json" \
     -d '{"email": "user@example.com", "password": "password123"}'
  1. Access protected endpoint:
curl -X GET "http://localhost:3000/api/v1/users/me" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
  1. Update user profile:
curl -X PUT "http://localhost:3000/api/v1/users/profile" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"email": "newemail@example.com"}'

Development

Available Scripts

  • npm start - Start the production server
  • npm run dev - Start development server with nodemon
  • npm run lint - Run ESLint
  • npm run lint:fix - Run ESLint with auto-fix

Project Structure

src/
├── config/         # Database configuration
├── controllers/    # Route controllers
├── middleware/     # Custom middleware
├── models/         # Sequelize models
├── routes/         # Express routes
├── utils/          # Utility functions
└── server.js       # Main server file