Skip to content

A TypeScript-based RESTful backend built with Express, PostgreSQL, and Sequelize, featuring JWT authentication, secure password hashing, session management, Swagger documentation, and clean layered architecture for scalable and maintainable development.

Notifications You must be signed in to change notification settings

koradiyamilan12/User-Authentication-Session-Management-API

Repository files navigation

TypeScript Backend Project

A robust RESTful API backend built with TypeScript, Express, Sequelize, and PostgreSQL. This project implements user authentication, session management, and follows clean architecture principles with proper separation of concerns.

🚀 Features

  • TypeScript: Fully typed codebase for better developer experience and code quality
  • Express.js: Fast, unopinionated web framework for Node.js
  • PostgreSQL: Powerful, open-source relational database
  • Sequelize ORM: Promise-based Node.js ORM for PostgreSQL
  • JWT Authentication: Secure token-based authentication
  • Password Hashing: Bcrypt for secure password storage
  • API Documentation: Interactive Swagger/OpenAPI documentation
  • Error Handling: Centralized error handling middleware
  • Logging: Winston logger for application logging
  • ESLint & Prettier: Code quality and formatting tools
  • Environment Variables: Secure configuration management with dotenv

📋 Prerequisites

Before running this project, make sure you have the following installed:

  • Node.js (v18 or higher)
  • npm or yarn
  • PostgreSQL (v12 or higher)

🛠️ Installation

  1. Clone the repository

    git clone <repository-url>
    cd backend
  2. Install dependencies

    npm install
  3. Set up environment variables

    Copy the .env.example file to .env:

    cp .env.example .env

    Update the .env file with your configuration:

    PORT=5000
    
    SECRET_KEY=your-jwt-secret-key
    EXPIRE_TIME=24h
    
    DB_HOST=localhost
    DB_PORT=5432
    DB_USER=your-database-username
    DB_PASSWORD=your-database-password
    DB_NAME=your-database-name
  4. Create PostgreSQL database

    createdb your-database-name

    Or use your preferred PostgreSQL client to create the database.

🚀 Running the Application

Development Mode

npm run dev

The server will start on http://localhost:5000 (or your configured PORT) with hot-reloading enabled.

Production Mode

npm run build
npm start

📚 API Documentation

Once the server is running, access the interactive API documentation at:

http://localhost:5000/api-docs

🏗️ Project Structure

backend/
├── src/
│   ├── config/          # Configuration files (database, logger)
│   ├── constants/       # Application constants and messages
│   ├── controllers/     # Request handlers
│   ├── docs/            # API documentation (Swagger/OpenAPI)
│   ├── interfaces/      # TypeScript interfaces
│   ├── middlewares/     # Express middlewares (auth, error handling)
│   ├── models/          # Sequelize models
│   ├── repository/      # Data access layer
│   ├── routes/          # API routes
│   ├── services/        # Business logic layer
│   ├── utils/           # Utility functions and helpers
│   ├── app.ts           # Express app configuration
│   └── server.ts        # Application entry point
├── .env.example         # Environment variables template
├── .gitignore          # Git ignore rules
├── eslint.config.ts    # ESLint configuration
├── package.json        # Project dependencies and scripts
├── tsconfig.json       # TypeScript configuration
└── README.md           # Project documentation

🔑 Available Scripts

Script Description
npm run dev Start development server with hot-reloading
npm start Start production server
npm run build Compile TypeScript to JavaScript
npm run lint Run ESLint to check code quality
npm run lint:fix Run ESLint and automatically fix issues

🛣️ API Endpoints

User Routes (/api/v1/user)

  • User registration
  • User profile management
  • User CRUD operations

Session Routes (/api/v1/session)

  • User login
  • User logout
  • Session management
  • Token refresh

For detailed endpoint documentation, please refer to the Swagger documentation at /api-docs.

🏛️ Architecture

This project follows a layered architecture pattern:

  1. Routes Layer: Defines API endpoints and maps them to controllers
  2. Controllers Layer: Handles HTTP requests and responses
  3. Services Layer: Contains business logic
  4. Repository Layer: Handles data access and database operations
  5. Models Layer: Defines database schemas using Sequelize
  6. Middlewares: Cross-cutting concerns (authentication, error handling)
  7. Utils: Reusable utility functions

🔒 Security Features

  • Password Hashing: Passwords are hashed using bcrypt before storage
  • JWT Authentication: Secure token-based authentication
  • Environment Variables: Sensitive data stored in environment variables
  • Input Validation: Request validation to prevent malicious input
  • Error Handling: Secure error messages that don't expose sensitive information

🧪 Testing

Note: Testing setup is not yet implemented. Consider adding:

  • Unit tests with Jest
  • Integration tests
  • E2E tests

📝 Code Quality

This project uses:

  • ESLint: For code linting and maintaining code standards
  • Prettier: For consistent code formatting
  • TypeScript: For type safety and better developer experience

Run linting:

npm run lint

Auto-fix linting issues:

npm run lint:fix

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the ISC License.

👤 Author

Milan

🙏 Acknowledgments

  • Express.js team for the excellent web framework
  • Sequelize team for the powerful ORM
  • TypeScript team for making JavaScript development better

Happy Coding! 🚀

About

A TypeScript-based RESTful backend built with Express, PostgreSQL, and Sequelize, featuring JWT authentication, secure password hashing, session management, Swagger documentation, and clean layered architecture for scalable and maintainable development.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published