Skip to content

Repository files navigation

Mini Auth Service

A lightweight authentication and API key management service built with NestJS, PostgreSQL, and JWT.
Designed for projects that need both user authentication and service-to-service API key access.

Features

  • User Authentication (JWT)

    • Register / Login
    • Secure password hashing
    • JWT-based session handling
  • API Key System

    • Users can generate API keys
    • Keys are hashed using HMAC-SHA256 and stored securely
    • Optional TTL (auto-expiry)
    • Revocation system
    • One-time return of the plain API key
  • Dual Auth Support

    • Bearer <token> → User access
    • ApiKey <key> or x-api-key → Service access
  • Guards

    • UserGuard() — only logged-in users
    • ServiceGuard() — API key clients
    • AnyAuthGuard() — either user or service

Tech Stack

  • NestJS (Core framework)
  • TypeORM (ORM)
  • PostgreSQL (Database)
  • JWT (User authentication)
  • Crypto (API key hashing)

API Overview

Authentication

  • POST /auth/register
  • POST /auth/login

API Keys

  • POST /keys/create — create a new API key
  • GET /keys/list — list user-owned keys
  • POST /keys/revoke/:id — revoke a key

Project Structure

src/
 ├── auth/               # JWT login/register logic
 ├── keys/               # API key entity, service, controller
 ├── users/              # User entity + user logic
 ├── common/
 │     ├── auth.middleware.ts
 │     ├── auth.guard.ts
 │     └── guards.ts
 ├── app.module.ts
 └── main.ts

Environment Variables

Create a .env file:

DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE=authdb

JWT_SECRET=your_jwt_secret_here
API_KEY_SALT=your_api_key_salt_here

Running the Project

npm install
npm run start:dev

Ensure PostgreSQL is running and the environment variables are set.

Notes

  • API keys cannot be recovered once created — only the hashed version is stored.
  • Make sure to keep JWT_SECRET and API_KEY_SALT private.
  • Suitable for personal projects, microservices, or learning real-world auth systems.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages