NestAuth is a secure, modular NestJS backend for authentication, user management, and application logging. It is designed for modern web applications that need a robust auth flow with email verification, password reset, JWT-based sessions, throttling, and admin-oriented user operations.
This project provides a complete authentication layer with:
The API is versioned under /v1 and is protected with validation, CORS configuration, rate limiting, and security headers.
Before running the project, make sure you have:
git clone https://github.com/S1mon009/NestAuth.git
cd NestAuth
npm installCreate a .env file in the project root with the following values. A ready-to-copy example is also available in env.example:
MONGO_URI=mongodb://localhost:27017/nestauth
JWT_SECRET=supersecretkey
JWT_EXPIRES_IN=86400
JWT_RESET_PASSWORD_EXPIRES_IN=900
REFRESH_TOKEN_SECRET=anothersecretkey
REFRESH_TOKEN_EXPIRES_IN=604800
COOKIE_SECURE=false
COOKIE_SAME_SITE=strict
SMTP_HOST=smtp.mailtrap.io
SMTP_PORT=2525
SMTP_USER=YOUR_MAILTRAP_USERNAME
SMTP_PASS=YOUR_MAILTRAP_PASSWORD
FRONTEND_URL=http://localhost:4200
CORS_ORIGIN=http://localhost:4200
API_VERSION=v1
PORT=3000
HOST=127.0.0.1
CACHE_TTL=5000
BCRYPT_SALT=12Important:
FRONTEND_URLis required for password reset links. For email verification, the application can fall back to a built-in render view on the backend whenFRONTEND_URLis not defined.
Build the production image from the repository root:
Example :docker build -t nestauth:latest -f docker/Dockerfile .Run the container with the project environment file:
Example :docker run --rm -d -p 3000:3000 --env-file .env --name nestauth nestauth:latestThe application will be available at:
Example :http://localhost:3000Swagger UI is available at:
Example :http://localhost:3000/apiUseful commands:
Example :docker logs nestauth
docker ps
docker rm -f nestauthNote: the container is configured to listen on
0.0.0.0, so it is reachable from outside the container. If your MongoDB instance runs on the host machine, use a Docker-friendly host value such ashost.docker.internalinMONGO_URI.
Start the development server:
Example :npm run start:devThe backend will be available at:
Example :http://localhost:3000Swagger UI is available at:
Example :http://localhost:3000/apiCompodoc documentation is also generated under the documentation/ folder.
Base URL:
Example :http://localhost:3000/v1POST /auth/registerGET /auth/verify-email?token=<token>POST /auth/loginPOST /auth/refresh-tokenPOST /auth/forgot-passwordPOST /auth/verify-reset-password?token=<token>POST /auth/reset-password?token=<token>GET /users/meGET /users/all (admin only)POST /users/add (admin only)GET /users/:id (admin only)GET /users/profile/:idPATCH /users/profile/:idPATCH /users/:id/role (admin only)GET /logs (admin only)GET /logs/user/:id (admin only)The project uses FRONTEND_URL when composing email links for:
${FRONTEND_URL}/auth/verify-email?token=...${FRONTEND_URL}/auth/reset-password?token=...If FRONTEND_URL is not defined, the verification flow still works because the application renders a default verification view on the backend for /auth/verify-email. For password reset, however, FRONTEND_URL is required, because the reset link must point to a frontend page that can collect the new password.
This means your frontend application should expose matching routes for these flows. In a typical setup:
/auth/verify-email?token=... handles the verification flow/auth/reset-password?token=... handles the reset password flowThese links are generated by the email service and are sent to users after registration or password reset requests. The backend itself also exposes the corresponding API endpoints, but the frontend routes are the recommended integration point for user-facing flows.
POST /auth/register.FRONTEND_URL when available; otherwise the backend renders the default verification view./auth/verify-email?token=... to complete the confirmation flow when using a dedicated frontend page./auth/reset-password?token=... after the reset email is received.If you want to use the backend directly, the corresponding API routes are also available under /v1/auth/verify-email and /v1/auth/reset-password.
npm run start — start the applicationnpm run start:dev — start in development mode with watch modenpm run start:prod — run the compiled production buildnpm run build — compile the TypeScript projectnpm run lint — run ESLintnpm run lint:fix — auto-fix lint issuesnpm run format — format the codebase with Prettiernpm run test — run Jest testsnpm run test:watch — run tests in watch modenpm run test:cov — run tests with coveragenpm run compodoc — generate and serve Compodoc documentationRun the test suite:
Example :npm run testGenerate the static documentation bundle:
Example :npm run compodocThe generated docs are stored in the documentation/ directory.