Skip to content

About

NestJS authentication with PostgreSQL, Prisma, JWT and Redis sessions, plus a step-by-step tutorial

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

postgres-auth

Authentication and authorization in NestJS, done properly: hashed passwords, JWTs, Redis-backed sessions and role guards on top of PostgreSQL + Prisma.

It also includes a step-by-step tutorial in tutorial/ that explains how each part was built.

Features

  • User registration and login with bcrypt password hashing
  • JWT access tokens (configurable expiry)
  • Session storage and validation in Redis
  • Role-based access control with guards, plus @Public(), @Roles() and @User() decorators
  • Prisma ORM on PostgreSQL

Tech stack

NestJS · TypeScript · Prisma · PostgreSQL · Redis · Passport JWT · Docker Compose

Tutorial

  1. Setup and fundamentals
  2. Password security
  3. JWT tokens deep dive
  4. Session management
  5. Authentication flow
  6. Authorization and guards
  7. Advanced features
  8. Production ready

API overview

Method Path Description
POST /users Register
POST /users/login Log in
GET /users/me, /users/profile Current user
GET /users, /users/:id List / get users
PATCH /users/:id Update a user

Getting started

docker compose up -d      # Postgres + Redis
npm install
npx prisma migrate dev
npm run start:dev

Environment variables: DATABASE_URL, JWT_SECRET, JWT_EXPIRES_IN, REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, PORT.

About

NestJS authentication with PostgreSQL, Prisma, JWT and Redis sessions, plus a step-by-step tutorial

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages