This is a RESTful API for a blogging platform built with NestJS, TypeORM, and PostgreSQL. The API supports user authentication, creating posts, and managing user roles with JWT-based authentication.
- Features
- Prerequisites
- Installation
- Configuration
- Running the Application
- API Endpoints
- Usage
- License
- User authentication (register, login, logout)
- Role-based access control (admin, user)
- CRUD operations for posts
- JWT-based authentication with HTTP-only cookies
- Swagger API documentation
Before you begin, ensure you have met the following requirements:
- Node.js (>= 12.x)
- npm (>= 6.x)
- PostgreSQL (>= 9.x)
-
Clone the repository:
git clone https://github.com/yourusername/your-repo.git cd your-repo -
Install dependencies:
npm install
- POST /auth/register: Register a new user.
- POST /auth/login: Login and obtain a JWT token.
- POST /auth/logout: Logout and clear the JWT token.
- GET /users: Get all users (admin only).
- GET /users/:id: Get a user by ID (admin only).
- GET /posts: Get all posts.
- GET /posts/:id: Get a post by ID.
- POST /posts: Create a new post (authenticated users).
- PUT /posts/:id: Update a post (only the owner).
- DELETE /posts/:id: Delete a post (only the owner).
The API documentation is available via Swagger. Once the application is running, navigate to http://localhost:3000/api to view the API documentation and test the endpoints.
- Register: Create a new user by sending a POST request to
/auth/registerwithusernameandpassword. - Login: Authenticate by sending a POST request to
/auth/loginwithusernameandpassword. The server responds with an HTTP-only cookie containing the JWT token. - Authenticated Requests: Include the JWT token in the
Authorizationheader as a Bearer token or rely on the HTTP-only cookie for authenticated requests. - Logout: Clear the JWT token by sending a POST request to
/auth/logout.
curl -X POST http://localhost:3000/auth/register \
-H "Content-Type: application/json" \
-d '{"username": "newuser", "password": "password"}'