$ show leosuncin/nest-auth-example
This repository implements a RESTful API for a simple todo application with user authentication, built using NestJS, Passport, TypeORM, and PostgreSQL. It provides registration and login (local strategy with JWT), session management via cookies, and CRUD operations for todos and user profiles. The architecture is a monolithic NestJS application with clearly separated modules (Auth, Todo, User) and uses standard patterns such as guards, interceptors, pipes, and filters. The codebase is well-tested with unit and end-to-end tests, includes stress testing with k6, and follows modern TypeScript practices with decorators and dependency injection. It serves as a practical example of integrating Passport authentication (local and JWT strategies) with NestJS and TypeORM, including custom validators, pagination interceptors, and ownership checks.
The LLM identified 3 files worth highlighting for reuse:
A generic NestJS interceptor that transforms a pair [items[], totalCount] into a standardized paginated response object containing the items, meta information (itemCount, totalItems, itemsPerPage, totalPages, currentPage), and HATEOAS-like links (first, previous, next, last). It uses class-transformer to instantiate DTOs and is fully parameterized, making it directly reusable in any NestJS project that needs paginated endpoints. The interceptor reads query parameters (limit, page) from the request and constructs absolute URLs for navigation links.
A NestJS interceptor that automatically attaches a JWT token to the response after successful authentication (login/register). It sets the Authorization header and a signed httpOnly cookie ('token') using the AuthService to sign the token. This pattern decouples token generation from controllers and ensures consistent token delivery across auth endpoints. It is a clean example of using interceptors to modify the response stream and can be adapted for any authentication flow.
A TypeORM entity for the User model that implements automatic password hashing using bcrypt via @BeforeInsert and @BeforeUpdate lifecycle hooks. It also provides a checkPassword method for comparing plaintext passwords with the stored hash. The hashing is idempotent (checks if already hashed) and uses a salt round generated by bcrypt. This pattern is a well-established, reusable approach for secure password management in any application using TypeORM and can be extracted as a base class or mixin.