|
| 1 | + |
| 2 | +# Node Rest API + JWT in TypeScript |
| 3 | + |
| 4 | +- This is a simple **Node Rest Api** written in **Typescript**. |
| 5 | +- Routes can be protected with **JWT tokens**. |
| 6 | +- Authentification with Passport. |
| 7 | + |
| 8 | +# How it works |
| 9 | + |
| 10 | +- The API dispatches requests with well structured **routes**. |
| 11 | +- Routes are using **controllers** for API implementations. |
| 12 | +- Controllers are using **models** for Mongo persistence. |
| 13 | +- Routes can be protected with **JWT authentification middelwares** : |
| 14 | +```typescript |
| 15 | +import { Router } from "express"; |
| 16 | +import { ProductController } from "../controllers/productController"; |
| 17 | +import { AuthController } from "../controllers/authController"; |
| 18 | + |
| 19 | + |
| 20 | +export class ProductRoutes { |
| 21 | + |
| 22 | + public router: Router; |
| 23 | + public productController: ProductController = new ProductController(); |
| 24 | + public authController: AuthController = new AuthController(); |
| 25 | + |
| 26 | + constructor() { |
| 27 | + this.router = Router(); |
| 28 | + this.routes(); |
| 29 | + } |
| 30 | + |
| 31 | + routes() { |
| 32 | + this.router.get("/", this.productController.getProducts); |
| 33 | + this.router.get("/:id", this.productController.getProduct); |
| 34 | + // The following routes are protected |
| 35 | + this.router.post("/", this.authController.authenticateJWT, this.productController.createProduct); |
| 36 | + this.router.put("/:id", this.authController.authenticateJWT, this.productController.updateProduct); |
| 37 | + this.router.delete("/:id", this.authController.authenticateJWT, this.productController.deleteProduct); |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +- Install dependencies |
| 43 | +``` |
| 44 | +cd rest-api-node-jwt-typescript |
| 45 | +npm install |
| 46 | +npm run build |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +# Getting started |
| 51 | + |
| 52 | + |
| 53 | +## Step1 : Register a user |
| 54 | +Send a POST request to `http://localhost:3000/api/user/register` |
| 55 | +with the following payload ** : |
| 56 | +```json |
| 57 | +{ |
| 58 | + "username": "me", |
| 59 | + "password": "pass" |
| 60 | +} |
| 61 | +``` |
| 62 | +You should get a JWT token in the response : |
| 63 | +```json |
| 64 | +{ |
| 65 | + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im1lMiIsImlhdCI6MTU1MDU4MTA4NH0.WN5D-BFLypnuklvO3VFQ5ucDjBT68R2Yc-gj8AlkRAs" |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Step2 : Create a Product |
| 70 | +Send a POST request to `http://localhost:3000/api/products` |
| 71 | +with the following payload : |
| 72 | +```json |
| 73 | +{ |
| 74 | + "productId": "13", |
| 75 | + "name": "Orange", |
| 76 | + "price": 5, |
| 77 | + "quantity": 6 |
| 78 | +} |
| 79 | +``` |
| 80 | +You should get an authorization **denied** ! |
| 81 | +```json |
| 82 | +{ |
| 83 | + "status": "error", |
| 84 | + "code": "unauthorized" |
| 85 | +} |
| 86 | +``` |
| 87 | +Add the JWT token to the Authorization header : |
| 88 | +```http |
| 89 | +Content-Type: application/json |
| 90 | +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im1lMiIsImlhdCI6MTU1MDU4MTA4NH0.WN5D-BFLypnuklvO3VFQ5ucDjBT68R2Yc-gj8AlkRAs |
| 91 | +``` |
| 92 | +You should have created the product !! |
| 93 | +```json |
| 94 | +{ |
| 95 | + "data": { |
| 96 | + "_id": "5c6c0845e3eb8302ffd168c0", |
| 97 | + "productId": "13", |
| 98 | + "name": "Orange", |
| 99 | + "price": 5, |
| 100 | + "quantity": 6, |
| 101 | + "__v": 0 |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | +## Step2 : Get a Product |
| 106 | +You can get the product with or without token because the Get route of Product router is not protected with the JWT authentification middelware. |
| 107 | +Send a GET request to `http://localhost:3000/api/products/13` |
| 108 | + |
| 109 | +You should get : |
| 110 | +```json |
| 111 | +[ |
| 112 | + { |
| 113 | + "_id": "5c6bfc97e3eb8302ffd168be", |
| 114 | + "productId": "13", |
| 115 | + "name": "Orange", |
| 116 | + "price": 5, |
| 117 | + "quantity": 6, |
| 118 | + "__v": 0 |
| 119 | + } |
| 120 | +``` |
0 commit comments