|
1 | 1 | # Backend-REST-API-JWT-Auth-RoleBased |
2 | 2 |
|
3 | | -A scalable RESTful backend service implementing JWT authentication, role-based access control, and CRUD operations for managing application data. |
| 3 | +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. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- User registration & login with **JWT** (stored in HTTP-only cookies) |
| 10 | +- Role-based access (`USER` and `ADMIN`) |
| 11 | +- Blog CRUD: users can manage their posts, admin can manage all posts |
| 12 | +- Secure password hashing with **bcrypt** |
| 13 | +- Docker-ready |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Tech Stack |
| 18 | + |
| 19 | +- Node.js + TypeScript |
| 20 | +- Express.js + Prisma (PostgreSQL) |
| 21 | +- JWT + bcrypt |
| 22 | +- Docker |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Environment Variables |
| 27 | + |
| 28 | +Create a `.env` file: |
| 29 | + |
| 30 | +```env |
| 31 | +DATABASE_URL="postgresql://username:password@host:port/database" |
| 32 | +JWT_SECRET="your-secret-key" |
| 33 | +PORT=4000 |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Run Locally |
| 39 | + |
| 40 | +```bash |
| 41 | +git clone https://github.com/raone1422g/restapi.git |
| 42 | +cd restapi |
| 43 | +pnpm install |
| 44 | +#make sure you have setup the env variable |
| 45 | +npx prisma generate |
| 46 | +npx prisma migrate dev --name init |
| 47 | +pnpm run dev |
| 48 | +``` |
| 49 | + |
| 50 | +API runs at: `http://localhost:4000` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Docker |
| 55 | + |
| 56 | +### Pull prebuilt image: |
| 57 | + |
| 58 | +```bash |
| 59 | +docker pull raone1422g/restapi:v1.0 |
| 60 | +``` |
| 61 | + |
| 62 | +### Run container: |
| 63 | + |
| 64 | +```bash |
| 65 | +docker run -d -p 3000:4000 \ |
| 66 | + -e DATABASE_URL="your_database_url" \ |
| 67 | + -e JWT_SECRET="your_jwt_secret" \ |
| 68 | + raone1422g/restapi:v1.0 |
| 69 | +``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## API Endpoints |
| 74 | + |
| 75 | +### Auth |
| 76 | + |
| 77 | +| Endpoint | Method | Description | |
| 78 | +| -------------------- | ------ | --------------------- | |
| 79 | +| `/api/auth/register` | POST | Register a user | |
| 80 | +| `/api/auth/login` | POST | Login & receive JWT | |
| 81 | +| `/api/auth/logout` | POST | Logout (clear cookie) | |
| 82 | +| `/api/auth/me` | GET | Get logged-in user | |
| 83 | + |
| 84 | +### Blogs |
| 85 | + |
| 86 | +| Endpoint | Method | Description | |
| 87 | +| ---------------------- | ------ | --------------------------- | |
| 88 | +| `/api/blog/allBlogs` | GET | Get all posts | |
| 89 | +| `/api/blog/getMyBlogs` | GET | Get logged-in user’s posts | |
| 90 | +| `/api/blog/create` | POST | Create a post | |
| 91 | +| `/api/blog/update/:id` | PUT | Update a post (author only) | |
| 92 | +| `/api/blog/delete/:id` | DELETE | Delete a post (author only) | |
| 93 | + |
| 94 | +### Users (Admin Only) |
| 95 | + |
| 96 | +| Endpoint | Method | Description | |
| 97 | +| ----------------------- | ------ | ------------------------ | |
| 98 | +| `/api/user/getAllUsers` | GET | Get all registered users | |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## Notes |
| 103 | + |
| 104 | +- All authenticated routes require the JWT cookie. |
| 105 | +- Admin-only routes are protected by role-based middleware. |
| 106 | +- Passwords are hashed, never stored in plaintext. |
| 107 | + |
| 108 | +--- |
0 commit comments