Skip to content

A scalable RESTful backend service implementing JWT authentication, role-based access control, and CRUD operations for managing application data.

Notifications You must be signed in to change notification settings

doTryCatch/Backend-REST-API-JWT-Auth-RoleBased

Repository files navigation

Backend-REST-API-JWT-Auth-RoleBased

A RESTful API backend built with TypeScript, Express, and Prisma, featuring JWT authentication and role-based access for user and blog management. Containerized with Docker for easy deployment.


Features

  • User registration & login with JWT (stored in HTTP-only cookies)
  • Role-based access (USER and ADMIN)
  • Blog CRUD: users can manage their posts, admin can manage all posts
  • Secure password hashing with bcrypt
  • Docker-ready

Tech Stack

  • Node.js + TypeScript
  • Express.js + Prisma (PostgreSQL)
  • JWT + bcrypt
  • Docker

Environment Variables

Create a .env file:

DATABASE_URL="postgresql://username:password@host:port/database"
JWT_SECRET="your-secret-key"
PORT=4000

Run Locally

git clone https://github.com/raone1422g/restapi.git
cd restapi
pnpm install
#make sure you have setup the env variable
npx prisma generate
npx prisma migrate dev --name init
pnpm run dev

API runs at: http://localhost:4000


Docker

Pull prebuilt image:

docker pull raone1422g/restapi:v1.0

Run container:

docker run -d -p 3000:4000 \
  -e DATABASE_URL="your_database_url" \
  -e JWT_SECRET="your_jwt_secret" \
  raone1422g/restapi:v1.0

API Endpoints

Auth

Endpoint Method Description
/api/auth/register POST Register a user
/api/auth/login POST Login & receive JWT
/api/auth/logout POST Logout (clear cookie)
/api/auth/me GET Get logged-in user

Blogs

Endpoint Method Description
/api/blog/allBlogs GET Get all posts
/api/blog/getMyBlogs GET Get logged-in user’s posts
/api/blog/create POST Create a post
/api/blog/update/:id PUT Update a post (author only)
/api/blog/delete/:id DELETE Delete a post (author only)

Users (Admin Only)

Endpoint Method Description
/api/user/getAllUsers GET Get all registered users

Notes

  • All authenticated routes require the JWT cookie.
  • Admin-only routes are protected by role-based middleware.
  • Passwords are hashed, never stored in plaintext.

About

A scalable RESTful backend service implementing JWT authentication, role-based access control, and CRUD operations for managing application data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published