Skip to content

Ikhan/NestJS-Authentication

Repository files navigation

NestJS Authentication Module

Nest Logo

NestJS TypeScript PostgreSQL JWT Drizzle ORM

A robust and scalable authentication microservice built with NestJS, featuring comprehensive user management, JWT-based authentication, and secure password handling.

✨ Features

  • 🔐 Complete Authentication System

    • User registration and login
    • JWT access and refresh token mechanism
    • Secure password hashing with bcrypt
    • Token refresh and revocation
  • 👥 User Management

    • User CRUD operations
    • Complete role and permission system with CASL
    • Email and username uniqueness validation
    • User profile management
    • Granular permission controls
  • 🛡️ Security First

    • Password strength validation
    • JWT token security with configurable expiration
    • Refresh token rotation
    • Secure database schema design
  • 🏗️ Modern Architecture

    • Modular NestJS structure
    • Drizzle ORM for type-safe database operations
    • PostgreSQL database with migrations
    • CASL for authorization and ability management
    • Clean separation of concerns
    • Swagger API documentation

Database Schema

  • Users: Core user information with assigned roles
  • Roles: Role definitions with associated permissions
  • Permissions: Granular permission definitions for actions and resources
  • Passwords: Secure password storage (hashed)
  • Refresh Tokens: JWT refresh token management

🚀 Installation & Setup

Prerequisites

  • Node.js >= 18
  • PostgreSQL >= 13
  • npm or yarn

Installation Steps

  1. Clone the repository
  2. Install dependencies:
npm install
  1. Set up PostgreSQL database
  2. Create and configure your .env file (see below)
  3. Run database migrations:
npm run db:migrate
  1. Seed the database (optional):
npm run db:seed
  1. Start the development server:
npm run start:dev

Environment Variables

Create a .env file in the root directory:

# Database
DATABASE_URL=postgresql://username:password@localhost:5432/auth_service

# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRES_IN=15m
JWT_REFRESH_SECRET=your-super-secret-refresh-key
JWT_REFRESH_EXPIRES_IN=7d

# Application
PORT=3000
NODE_ENV=development

🔒 Security Features

JWT Implementation

  • Access Tokens: Short-lived (15 minutes) for API access
  • Refresh Tokens: Long-lived (7 days) for token renewal
  • Token Rotation: Refresh tokens are rotated on each use
  • Secure Storage: Tokens use strong secrets and proper signing

Password Security

  • bcrypt Hashing: Industry-standard password hashing
  • Salt Rounds: Configurable salt rounds for performance tuning
  • Password Validation: Server-side validation for password strength

Database Security

  • Prepared Statements: Protection against SQL injection
  • Type Safety: Drizzle ORM ensures type-safe database operations
  • Schema Validation: Zod-based validation for all inputs

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add 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 UNLICENSED License.

Built with ❤️ using NestJS and TypeScript

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published