This is a template project for integrating authentication using NestJS, Prisma, and JWT. It includes:
- User management with Prisma and PostgreSQL
- JWT authentication (login, protected routes)
- Custom decorators (
@Auth(),@GetUser()) - Example public and protected routes
This project is intended as a starting point for building NestJS apps with authentication.
- Node.js >= 18
- Docker (for PostgreSQL)
- pnpm (recommended) or npm/yarn
- Clone the repository
git clone https://github.com/SebasDev807/nest-auth-kit.git
cd nest-auth-kit- Rename
.env.templateto.env
Copy .env.example to .env and update your credentials:
DATABASE_URL="postgresql://postgres:root@localhost:5432/auth_kit?schema=public"
PORT=3000
JWT_SECRET=5up3r_s3cr3t_1wt_k3y321
POSTGRES_USER=postgres
POSTGRES_PASSWORD=root
POSTGRES_DB=auth_kit
POSTGRES_PORT=5432- Start PostgreSQL using Docker
docker compose up -dMake sure the database is running:
docker ps- Install dependencies
pnpm install- Run Prisma migrations
pnpx prisma migrate dev --name init- Start the app
pnpm start:devThe API should now be running on http://localhost:3000/api/v1.
GET /example/publicPOST /users/register- Register a new user
- Request Body
{
"email": "user@example.com",
"password": "yourpassword"
}POST /auth/login- Login with existing account
- Request Body
{
"email": "user@example.com",
"password": "yourpassword"
}Returns a message without authentication.
GET /example/protected
Authorization: Bearer <JWT_TOKEN>Returns a message with user info. Requires a valid JWT token.
@Auth()→ applies JWT guard to a route@GetUser()→ extracts the authenticated user from the request
Example:
@Auth()
@Get('protected')
protectedRoute(@GetUser() user) {
return {
message: 'This is a protected route',
user
};
}- Ensure you always pass the JWT token in the
Authorizationheader asBearer <token>. AuthModuleandUsersModuleshould be properly imported in each module that uses@Auth().- This template is intended for learning and starting a project; adapt it as needed for production use.