This JWT Authentication API built with Express and PostgreSQL is a robust solution for building secure user authentication and authorization systems. It supports essential CRUD operations for managing user posts, JWT-based security, and a powerful backend powered by Sequelize ORM. This project is ideal for developers looking to integrate secure user authentication with PostgreSQL in their applications.
- JWT Authentication: Secure user login and registration with password hashing and JWT tokens.
- Role-Based Access Control: Control access to resources based on roles, enhancing security for protected routes.
- CRUD API: Perform Create, Read, Update, Delete operations for posts with full authentication and authorization.
- PostgreSQL Integration: Seamless integration with PostgreSQL using Sequelize ORM for data management.
- Express Middleware: Use authentication middleware to protect routes requiring authentication.
This project helps you quickly implement user authentication using JWT tokens, providing:
- Scalable authentication for both small and large applications.
- Secure access to protected endpoints.
- A seamless and easy-to-extend architecture using PostgreSQL.
Ideal for:
- Web applications needing a secure API.
- Full-stack projects where backend authentication is crucial.
- Developers looking for a practical implementation of JWT authentication.
- Node.js (v14 or higher) — Ensure you have Node.js installed on your system.
- PostgreSQL (v12 or higher) — Make sure you have PostgreSQL installed locally or remotely.
- Git (for cloning the repository) — You'll need Git to clone this repository to your local machine.
-
Clone the Repository
git clone https://github.com/davhsi/JWT-Auth-Express-Postgres-API.git cd JWT-Auth-Express-Postgres-API -
Install Dependencies
npm install
-
Set Up Environment Variables
Create a.envfile in the project root directory with the following content:DATABASE_URL=postgres://postgres:root@localhost:5432/postgres JWT_SECRET=topsecretkey123- DATABASE_URL: The PostgreSQL connection string, typically in the format
postgres://username:password@localhost:5432/database. - JWT_SECRET: A strong secret key for JWT token signing.
- DATABASE_URL: The PostgreSQL connection string, typically in the format
-
Run the Application Start the application with:
npm start
The server will now be running on
http://localhost:5000. -
Using Nodemon (optional)
To automatically restart the server on file changes, usenodemon:nodemon server.js
- Endpoint:
POST /api/auth/register - Request Body:
{ "username": "yourusername", "password": "yourpassword" } - Response: Success message or error details.
- Endpoint:
POST /api/auth/login - Request Body:
{ "username": "yourusername", "password": "yourpassword" } - Response:
{ "token": "JWT_TOKEN" }
- Endpoint:
POST /api/posts/ - Headers:
Authorization: Bearer JWT_TOKEN - Request Body:
{ "title": "Post Title", "description": "Post Description" } - Response:
{ "id": 1, "title": "Post Title", "description": "Post Description", "author": 1, "updatedAt": "2024-11-14T08:57:03.722Z", "createdAt": "2024-11-14T08:57:03.722Z" }
- Endpoint:
GET /api/posts/ - Response:
[ { "id": 1, "title": "Post Title", "description": "Post Description", "author": 1 }, ... ]
- Endpoint:
GET /api/posts/:id - Response: Details of a specific post.
- Endpoint:
PUT /api/posts/:id - Headers:
Authorization: Bearer JWT_TOKEN - Request Body:
{ "title": "Updated Title", "description": "Updated Description" } - Response: Updated post details.
- Endpoint:
DELETE /api/posts/:id - Headers:
Authorization: Bearer JWT_TOKEN - Response: Success message.
- controllers: Business logic for each endpoint
- models: Sequelize models for
UserandPost - routes: Routing files for
authandposts - middleware: Authentication middleware for protected routes
This project connects to a PostgreSQL database using Sequelize ORM. The database configuration is provided through the DATABASE_URL environment variable, which must be set in the .env file.
- Ensure that PostgreSQL is running and the database is accessible.
- This API is scalable, and you can easily integrate it with other parts of your application.
With this JWT Authentication API, you get a secure, robust foundation for building applications that require authentication and authorization. It's ready to be extended with additional features such as role management, admin functionality, or even OAuth integration.
- JWT Authentication
- Express API
- PostgreSQL API
- Sequelize ORM
- Full-stack Authentication
- Secure API
- RESTful API