This is a basic authentication application built using NestJS, Prisma ORM, PostgreSQL, and Docker. This project aims to demonstrate how to set up user authentication with NestJS and Prisma, using PostgreSQL as the database and Docker for containerization.
- User registration
- User login
- JWT authentication
- CRUD operations for user management
- Secure password hashing
Make sure you have the following installed:
- Node.js (v14 or later)
- Docker
- Docker Compose
- Git
- PostgreSQL (if not using Docker)
```bash
git clone https://github.com/your-username/auth-nestjs-jwt.git
cd auth-nestjs-jwt
```
Create a docker-compose.yml file in the project root with the following content:
```yaml
version: '3.8'
services:
postgres:
image: postgres:13
container_name: auth_postgres
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: auth_db
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
```
Start the PostgreSQL container:
```bash
docker-compose up -d
```
Install the project dependencies:
```bash
npm install
```
Generate the Prisma client:
```bash
npx prisma generate
```
Run database migrations:
```bash
npx prisma migrate dev --name init
```
Create a .env file in the project root with the following content:
```env
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/auth_db?schema=public"
JWT_SECRET="your_jwt_secret"
```
Start the NestJS application:
```bash
npm run start:dev
```
To run the tests, use the following command:
```bash
npm run test
```
- POST /auth/register - Register a new user
- POST /auth/login - Authenticate a user and receive a JWT
- GET /users - Get all users (protected)
- GET /users/:id - Get user by ID (protected)
- PUT /users/:id - Update user by ID (protected)
- DELETE /users/:id - Delete user by ID (protected)
```arduino
src/
├── auth/
│ ├── auth.controller.ts
│ ├── auth.module.ts
│ ├── auth.service.ts
│ ├── dto/
│ ├── guards/
│ └── strategies/
├── users/
│ ├── users.controller.ts
│ ├── users.module.ts
│ ├── users.service.ts
│ ├── dto/
│ └── entities/
├── prisma/
│ ├── prisma.module.ts
│ └── prisma.service.ts
├── main.ts
└── app.module.ts
```
- Fork the repository
- Create a new branch (git checkout -b feature/your-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin feature/your-feature)
- Create a new Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you have any questions, feel free to contact me at [email protected]