Skip to content

Chirag Auth Validator is an all-in-one, developer-friendly solution for OTP-based email validation and JWT-based authentication. Can be used as a npm package too.

License

Notifications You must be signed in to change notification settings

ThisChirag/chirag-auth-validator

Repository files navigation

Chirag Auth Validator πŸ”§

A robust Node.js/TypeScript authentication system providing:

  • Secure OTP generation/validation
  • JWT-based authentication
  • Password reset flows
  • Prisma-based database integration
  • Redis caching for OTP/session management

NPM

πŸ“¦ NPM Package

Available on NPM!
You can install this package using either npm or pnpm:

npm install @thischirag/auth
pnpm add @thischirag/auth

For more information, refer to the NPM Package Documentation or NPM-Package.md.

πŸ“™ Table of Contents

  1. Overview
  2. Features
  3. Requirements
  4. Environment Configuration
  5. Local Development
  6. Docker Deployment
  7. Email Services
  8. Scripts & Commands
  9. Testing via Swagger
  10. Contributing
  11. License
  12. Contact

πŸ”Ž Overview

Chirag Auth Validator is an all-in-one, developer-friendly solution for OTP-based email validation and JWT-based authentication. It uses PostgreSQL for persistent storage, Redis for caching, and integrates with Prisma for schema-based ORM functionality. The project also includes a Swagger UI hosted at auth.chiragcodes.com for easy API testing and documentation.


⚑ Features

  • OTP Generation & Validation
  • JWT Authentication
  • Password Reset
  • Prisma Integration
  • Redis Support
  • Flexible Email Providers

βœ… Requirements

  • Node.js (v16+ recommended)
  • pnpm (for package management)
  • PostgreSQL (local or via Docker)
  • Redis (local or via Docker)
  • Docker & Docker Compose (for containerized deployment)

πŸ› οΈ Environment Configuration

  1. Copy .env.example to .env:
    cp .env.example .env
  2. Set the Environment Mode:
    • For development:
      NODE_ENV="development"
    • For production:
      NODE_ENV="production"
  3. Fill in the required fields:
    • DATABASE_URL: Connection string for PostgreSQL.
    • REDIS_URL: Connection string for Redis.
    • JWT_SECRET: Secret key for JWT tokens.
    • RESEND_API_KEY: API key for Resend email service.
      • Note: Ensure your domain is verified before using the Resend API key.
    • YOUR_DOMAIN: Use the format [email protected] to ensure proper email delivery.
  4. Keep .env private:
    • Ensure .env is listed in .gitignore to avoid committing sensitive data.

πŸ§‘β€πŸ’» Local Development

Steps

  1. Install Dependencies:
    pnpm install
  2. Prisma Migrations & Generation:
    npx prisma migrate dev
    npx prisma generate
  3. Run Development Server:
    pnpm run dev
    This starts the app on PORT=8080 (by default) or whatever port you set in .env.

Database & Redis Setup

  • PostgreSQL (Local or Docker):

    docker run --name pg-container \
      -e POSTGRES_PASSWORD=mysecretpassword \
      -p 5432:5432 \
      -d postgres

    Or install locally and update .env:

    DATABASE_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres?schema=public"
  • Redis (Local or Docker):

    docker run --name redis-container \
      -p 6379:6379 \
      -d redis

    Or install locally and update .env:

    REDIS_URL="redis://localhost:6379"

🐳 Docker Deployment

Using Docker Compose

Docker Compose simplifies running multiple containers (PostgreSQL, Redis, and your application) together.

  1. Ensure Your .env File is Configured

    Update .env to reference the service names defined in docker-compose.yml:

    DATABASE_URL="postgresql://postgres:mysecretpassword@postgres:5432/postgres?schema=public"
    REDIS_URL="redis://redis:6379"
  2. Make the Entrypoint Script Executable Ensure the docker-entrypoint.sh file is executable:

    chmod +x docker-entrypoint.sh
  3. Run the Containers

    docker-compose up --build -d
  4. Apply Prisma Migrations

    docker-compose exec app pnpm prisma migrate deploy
    docker-compose exec app pnpm prisma generate
  5. Verify the Setup

    • Check Logs (Optional):
      docker-compose logs -f app
    • Stop the Containers:
      docker-compose down

βœ‰οΈ Email Services

  • Resend:
    • Set RESEND_API_KEY in .env.
    • Ensure your domain is verified before using the Resend API key.
    • Use the format [email protected] for YOUR_DOMAIN in .env to ensure proper email delivery.

🚜 Scripts & Commands

Command Description
pnpm install Install dependencies
pnpm run dev Start dev server on specified PORT
pnpm run build Compile TypeScript into dist/
pnpm run lint Run ESLint checks
pnpm run lint:fix Autofix lint errors where possible
pnpm run format Format code with Prettier
pnpm prisma migrate deploy Apply DB migrations in production
pnpm prisma generate Re-generate Prisma client
docker build -t <image> . Build Docker image from Dockerfile
docker-compose up --build -d Build & start all services (Postgres, Redis, app) in detached mode
docker-compose exec app <cmd> Execute <cmd> inside the app container
docker-compose logs -f app Follow the logs of the app container
docker-compose down Stop & remove all containers, networks, and volumes

πŸ”¬ Testing via Swagger

  1. Hosted Swagger

  2. Local Swagger

    • Update the servers: section in openapi.yml to:
      servers:
        - url: http://localhost:8080/api
          description: Local Development Server
    • Then navigate to http://localhost:8080/docs to test endpoints.

🧬 Contributing

  1. Fork the Repo

  2. Create a Branch

    git checkout -b feature/new-feature
  3. Make Changes & Test

  4. Commit & Push

    git commit -m "Add new feature"
    git push origin feature/new-feature
  5. Open a Pull Request on GitHub


πŸ“’ License

This project is licensed under the MIT License.


πŸ“§ Contact

For issues or suggestions, feel free to open a GitHub issue or reach out via email.

About

Chirag Auth Validator is an all-in-one, developer-friendly solution for OTP-based email validation and JWT-based authentication. Can be used as a npm package too.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published