Skip to content

Commit

Permalink
add jwt security in get-all-posts & delete-posts logic (client & serv…
Browse files Browse the repository at this point in the history
…er); add jwt payload validation & extraction in app; split logic into files in app b00tc4mp#136
  • Loading branch information
Eduardolans committed Jun 20, 2024
1 parent dca3f47 commit dc3d7d0
Show file tree
Hide file tree
Showing 90 changed files with 7,662 additions and 63 deletions.
9 changes: 7 additions & 2 deletions staff/eduardo-sanchez/socialcode/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
🐖 curl http://localhost:8080/users/manzana -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYW56YW5hIiwiaWF0IjoxNzE4ODAxNjAwLCJleHAiOjE3MTg4MDUyMDB9.sGk2sd-sR5oF5xZVyBkxIl3surLx4bkuq5GTMxptp0Y" -v
```

- list posts
- get all posts

```sh
🐖 curl http://localhost:8080/posts -v
🐖 curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwZXBpdG9ncmlsbG8iLCJpYXQiOjE3MTg4MTE5MTgsImV4cCI6MTcxODgxNTUxOH0.pbiutUd6MogOdsPE8YvLVigidyuAVQEPhegaj9Ena4w" http://localhost:8080/posts -v


🐖 curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwZXBpdG9ncmlsbG8iLCJpYXQiOjE3MTg3OTg5NjgsImV4cCI6MTcxODgwMjU2OH0.aX_xV3VSpHrbDqznlPfAORLCCEQlnxM-Zt8ubmStzA4" http://localhost:8080/posts -v


```

- create post
Expand Down
2 changes: 1 addition & 1 deletion staff/eduardo-sanchez/socialcode/api/data/posts.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"author":"pepitogrillo","title":"blah","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"blah blah","id":"5879244716997389-1716667549269","date":"2024-05-25T20:05:49.269Z"},{"id":"5424401351689265-1716995937318","author":"WendyD","title":"asfaF","image":"https://media.giphy.com/media/xTiTntKyFNFbCNuqkw/giphy.gif?cid=790b76117ksklq64slgwdawn5cmtm14enobheew3nkecwdq1&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"ASAFA","date":"2024-05-29T15:18:57.318Z"},{"author":"peterpan","title":"hello world","image":"https://miro.medium.com/v2/resize:fit:1024/1*OohqW5DGh9CQS4hLY5FXzA.png","description":"console.log(\"hello world\")","date":"2024-05-30T13:46:33.345Z","id":"2575794271140359-1717076793345"},{"author":"peterpan","title":"blah","image":"https://upload.wikimedia.org/wikipedia/commons/1/1d/Blah_Blah_Blah.jpg","description":"blah blah","date":"2024-05-30T14:07:06.222Z","id":"03238060139373644-1717078026222"},{"author":"peterpan","title":"i love js","image":"https://images-na.ssl-images-amazon.com/images/I/61VmREcLaFL._SLDPMOBCAROUSELAUTOCROP288221_MCnd_AC_SR462,693_.jpg","description":"t-shirt","date":"2024-05-30T15:07:38.101Z","id":"7052022051791447-1717081658101"},{"author":"ManoloC","title":"i love debugging","image":"https://ih1.redbubble.net/image.5079675350.2246/gptr,1400x,front,black-c,188,133,1000,1000-bg,f8f8f8.jpg","description":"otra tshirt","date":"2024-05-30T15:12:22.193Z","id":"3242603140750757-1717081942193"},{"author":"peterpan","title":"I hate onions","image":"https://res.cloudinary.com/teepublic/image/private/s--jk8hOZj1--/t_Resized%20Artwork/c_crop,x_10,y_10/c_fit,h_563/c_crop,g_north_west,h_626,w_470,x_-40,y_-15/g_north_west,u_upload:v1462829018:production:blanks:ekerz3afkzxin2pgqj8h,x_-435,y_-340/b_rgb:eeeeee/c_limit,f_auto,h_630,q_auto:good:420,w_630/v1704062586/production/designs/55814456_0.jpg","description":"camiseta","date":"2024-05-30T15:19:04.125Z","id":"4007381802005925-1717082344121"},{"author":"pepitogrillo","title":"this is me","image":"https://pastaypizzagrossi.com/wp-content/uploads/2015/11/pepito.png","description":"PepitoG","date":"2024-06-03T15:37:09.141Z","id":"7816694399029667-1717429029143"},{"author":"PabloP","title":"yo","image":"https://imgs.search.brave.com/shWZquMWkPDaIeX0KktOS6CA0nC8DjMZ7f8UNHr3FIc/rs:fit:500:0:0/g:ce/aHR0cHM6Ly9oaXBz/LmhlYXJzdGFwcHMu/Y29tL2htZy1wcm9k/L2ltYWdlcy9hdXRv/cnJldHRhcm8tMTkw/Ny0xNjU3ODA0Mzg5/LmpwZWc_Y3JvcD0x/eHc6MXhoO2NlbnRl/cix0b3AmcmVzaXpl/PTk4MDoq","description":"my portrait","date":"2024-06-03T16:49:06.088Z","id":"6226364135019973-1717433346091"},{"author":"peterpan","title":"NBA","image":"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOHJla2VldzRxODB3M25lZmw4eHo4aXBnb2dnOGZkNjR0aHAxZGw2MCZlcD12MV9naWZzX3RyZW5kaW5nJmN0PWc/q45TMknC4xNUCfZt6H/giphy.gif","description":"PlayOffs","date":"2024-06-05T14:54:30.056Z","id":"8142909801569895-1717599270057"},{"author":"WendyD","title":"dancing","image":"https://media.giphy.com/media/iibH5ymW6LFvSIVyUc/giphy.gif?cid=790b76118rekeew4q80w3nefl8xz8ipgogg8fd64thp1dl60&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"girl dancing","date":"2024-06-05T16:59:52.426Z","id":"9678571314947659-1717606792429"},{"author":"peterpan","title":"thursday","image":"https://media.giphy.com/media/H3NC55FLtoF3RXM3eL/giphy.gif?cid=790b7611yhopdjk154taitnd087agc4kk9f1mv61gmkvlv4l&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"hege","date":"2024-06-13T17:37:30.738Z","id":"15869690379310386-1718300250740"},{"author":"peterpan","title":"foto","image":"https://media.giphy.com/media/4Jxa0QgHF2HSw/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"look","date":"2024-06-17T08:35:29.759Z","id":"9106929267928583-1718613329760"},{"author":"peterpan","title":"carrey","image":"https://media.giphy.com/media/dRvEZLV0ORAmHT1L5u/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"jim","date":"2024-06-17T09:06:47.560Z","id":"35807982447313225-1718615207560"},{"author":"peterpan","title":"dfhdfhz","image":"https://media.giphy.com/media/UuB5lh1bL1Dl6svihe/giphy.gif?cid=82a1493bljazeirf80bswzzbztripbnc5hfph6gll89r3spz&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"hugs","date":"2024-06-17T11:31:15.209Z","id":"27637147804292184-1718623875210"},{"author":"peterpan","title":"vaqueroooo","image":"https://media.giphy.com/media/SAUOBJogCbegrukfwR/giphy.gif?cid=82a1493b1bk1hxb7r6ubjt02jfxyvwxpt4sqx0x3j3h0zc03&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"yayyyy!","date":"2024-06-17T11:49:05.188Z","id":"381087757154702-1718624945188"},{"author":"ManoloC","title":"ghjkgh","image":"https://media.giphy.com/media/n1rwdMK9xlxxNNMyQo/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"dfhdzfh","date":"2024-06-18T14:08:39.919Z","id":"9633525110445467-1718719719920"},{"author":"pepitogrillo","title":"jeje","image":"https://media.giphy.com/media/Z21HJj2kz9uBG/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"kiss","date":"2024-06-18T14:40:47.925Z","id":"24161165284086827-1718721647927"},{"author":"manzana","title":"asd","image":"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdXc4OWU0ZHBvNHN2YWk3cTZzdWxoNHU3eWVhdWRxaXBzZTBpOGQybyZlcD12MV9naWZzX3RyZW5kaW5nJmN0PWc/TSHnt3RWiyHMFQfEfb/giphy.gif","description":"yeyyy","date":"2024-06-19T14:07:26.245Z","id":"9557888858059542-1718806046245"},{"author":"manzana","title":"sdfsd","image":"https://media.giphy.com/media/n1rwdMK9xlxxNNMyQo/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"sda","date":"2024-06-19T14:31:27.502Z","id":"7817673189885932-1718807487503"},{"author":"manzana","title":"Asda","image":"https://media.giphy.com/media/dRvEZLV0ORAmHT1L5u/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"asS","date":"2024-06-19T14:32:27.030Z","id":"6097778961636684-1718807547030"},{"author":"peterpan","title":"gvhghj","image":"https://media.giphy.com/media/Ut8FZWMUJW0bv9D4yp/giphy.gif?cid=82a1493bovf54f44fmw2x87b67hna5ob4q1k2xu3o9bjn088&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"fhddf","date":"2024-06-19T14:50:12.739Z","id":"21922868041727006-1718808612739"},{"author":"pepitogrillo","title":"blah","image":"https://upload.wikimedia.org/wikipedia/commons/1/1d/Blah_Blah_Blah.jpg","description":"blah blah","date":"2024-06-19T14:58:34.390Z","id":"7876550578905213-1718809114390"}]
[{"author":"pepitogrillo","title":"blah","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"blah blah","id":"5879244716997389-1716667549269","date":"2024-05-25T20:05:49.269Z"},{"id":"5424401351689265-1716995937318","author":"WendyD","title":"asfaF","image":"https://media.giphy.com/media/xTiTntKyFNFbCNuqkw/giphy.gif?cid=790b76117ksklq64slgwdawn5cmtm14enobheew3nkecwdq1&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"ASAFA","date":"2024-05-29T15:18:57.318Z"},{"author":"peterpan","title":"hello world","image":"https://miro.medium.com/v2/resize:fit:1024/1*OohqW5DGh9CQS4hLY5FXzA.png","description":"console.log(\"hello world\")","date":"2024-05-30T13:46:33.345Z","id":"2575794271140359-1717076793345"},{"author":"peterpan","title":"blah","image":"https://upload.wikimedia.org/wikipedia/commons/1/1d/Blah_Blah_Blah.jpg","description":"blah blah","date":"2024-05-30T14:07:06.222Z","id":"03238060139373644-1717078026222"},{"author":"peterpan","title":"i love js","image":"https://images-na.ssl-images-amazon.com/images/I/61VmREcLaFL._SLDPMOBCAROUSELAUTOCROP288221_MCnd_AC_SR462,693_.jpg","description":"t-shirt","date":"2024-05-30T15:07:38.101Z","id":"7052022051791447-1717081658101"},{"author":"ManoloC","title":"i love debugging","image":"https://ih1.redbubble.net/image.5079675350.2246/gptr,1400x,front,black-c,188,133,1000,1000-bg,f8f8f8.jpg","description":"otra tshirt","date":"2024-05-30T15:12:22.193Z","id":"3242603140750757-1717081942193"},{"author":"peterpan","title":"I hate onions","image":"https://res.cloudinary.com/teepublic/image/private/s--jk8hOZj1--/t_Resized%20Artwork/c_crop,x_10,y_10/c_fit,h_563/c_crop,g_north_west,h_626,w_470,x_-40,y_-15/g_north_west,u_upload:v1462829018:production:blanks:ekerz3afkzxin2pgqj8h,x_-435,y_-340/b_rgb:eeeeee/c_limit,f_auto,h_630,q_auto:good:420,w_630/v1704062586/production/designs/55814456_0.jpg","description":"camiseta","date":"2024-05-30T15:19:04.125Z","id":"4007381802005925-1717082344121"},{"author":"pepitogrillo","title":"this is me","image":"https://pastaypizzagrossi.com/wp-content/uploads/2015/11/pepito.png","description":"PepitoG","date":"2024-06-03T15:37:09.141Z","id":"7816694399029667-1717429029143"},{"author":"PabloP","title":"yo","image":"https://imgs.search.brave.com/shWZquMWkPDaIeX0KktOS6CA0nC8DjMZ7f8UNHr3FIc/rs:fit:500:0:0/g:ce/aHR0cHM6Ly9oaXBz/LmhlYXJzdGFwcHMu/Y29tL2htZy1wcm9k/L2ltYWdlcy9hdXRv/cnJldHRhcm8tMTkw/Ny0xNjU3ODA0Mzg5/LmpwZWc_Y3JvcD0x/eHc6MXhoO2NlbnRl/cix0b3AmcmVzaXpl/PTk4MDoq","description":"my portrait","date":"2024-06-03T16:49:06.088Z","id":"6226364135019973-1717433346091"},{"author":"peterpan","title":"NBA","image":"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOHJla2VldzRxODB3M25lZmw4eHo4aXBnb2dnOGZkNjR0aHAxZGw2MCZlcD12MV9naWZzX3RyZW5kaW5nJmN0PWc/q45TMknC4xNUCfZt6H/giphy.gif","description":"PlayOffs","date":"2024-06-05T14:54:30.056Z","id":"8142909801569895-1717599270057"},{"author":"WendyD","title":"dancing","image":"https://media.giphy.com/media/iibH5ymW6LFvSIVyUc/giphy.gif?cid=790b76118rekeew4q80w3nefl8xz8ipgogg8fd64thp1dl60&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"girl dancing","date":"2024-06-05T16:59:52.426Z","id":"9678571314947659-1717606792429"},{"author":"peterpan","title":"thursday","image":"https://media.giphy.com/media/H3NC55FLtoF3RXM3eL/giphy.gif?cid=790b7611yhopdjk154taitnd087agc4kk9f1mv61gmkvlv4l&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"hege","date":"2024-06-13T17:37:30.738Z","id":"15869690379310386-1718300250740"},{"author":"peterpan","title":"foto","image":"https://media.giphy.com/media/4Jxa0QgHF2HSw/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"look","date":"2024-06-17T08:35:29.759Z","id":"9106929267928583-1718613329760"},{"author":"peterpan","title":"carrey","image":"https://media.giphy.com/media/dRvEZLV0ORAmHT1L5u/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"jim","date":"2024-06-17T09:06:47.560Z","id":"35807982447313225-1718615207560"},{"author":"peterpan","title":"dfhdfhz","image":"https://media.giphy.com/media/UuB5lh1bL1Dl6svihe/giphy.gif?cid=82a1493bljazeirf80bswzzbztripbnc5hfph6gll89r3spz&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"hugs","date":"2024-06-17T11:31:15.209Z","id":"27637147804292184-1718623875210"},{"author":"peterpan","title":"vaqueroooo","image":"https://media.giphy.com/media/SAUOBJogCbegrukfwR/giphy.gif?cid=82a1493b1bk1hxb7r6ubjt02jfxyvwxpt4sqx0x3j3h0zc03&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"yayyyy!","date":"2024-06-17T11:49:05.188Z","id":"381087757154702-1718624945188"},{"author":"ManoloC","title":"ghjkgh","image":"https://media.giphy.com/media/n1rwdMK9xlxxNNMyQo/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"dfhdzfh","date":"2024-06-18T14:08:39.919Z","id":"9633525110445467-1718719719920"},{"author":"pepitogrillo","title":"jeje","image":"https://media.giphy.com/media/Z21HJj2kz9uBG/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"kiss","date":"2024-06-18T14:40:47.925Z","id":"24161165284086827-1718721647927"},{"author":"manzana","title":"asd","image":"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdXc4OWU0ZHBvNHN2YWk3cTZzdWxoNHU3eWVhdWRxaXBzZTBpOGQybyZlcD12MV9naWZzX3RyZW5kaW5nJmN0PWc/TSHnt3RWiyHMFQfEfb/giphy.gif","description":"yeyyy","date":"2024-06-19T14:07:26.245Z","id":"9557888858059542-1718806046245"},{"author":"manzana","title":"sdfsd","image":"https://media.giphy.com/media/n1rwdMK9xlxxNNMyQo/giphy.gif?cid=790b7611uw89e4dpo4svai7q6sulh4u7yeaudqipse0i8d2o&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"sda","date":"2024-06-19T14:31:27.502Z","id":"7817673189885932-1718807487503"},{"author":"peterpan","title":"gvhghj","image":"https://media.giphy.com/media/Ut8FZWMUJW0bv9D4yp/giphy.gif?cid=82a1493bovf54f44fmw2x87b67hna5ob4q1k2xu3o9bjn088&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"fhddf","date":"2024-06-19T14:50:12.739Z","id":"21922868041727006-1718808612739"},{"author":"pepitogrillo","title":"blah","image":"https://upload.wikimedia.org/wikipedia/commons/1/1d/Blah_Blah_Blah.jpg","description":"blah blah","date":"2024-06-19T14:58:34.390Z","id":"7876550578905213-1718809114390"},{"author":"peterpan","title":"jjkk","image":"https://media.giphy.com/media/Ut8FZWMUJW0bv9D4yp/giphy.gif?cid=82a1493bovf54f44fmw2x87b67hna5ob4q1k2xu3o9bjn088&ep=v1_gifs_trending&rid=giphy.gif&ct=g","description":"hhhh","date":"2024-06-20T09:08:29.502Z","id":"6847967960410499-1718874509505"},{"author":"dana","title":"yo","image":"https://media.giphy.com/media/MtsBHd0UsiH7t6twq0/giphy.gif?cid=ecf05e47lglfhzk0hbmna8m51kdi2kevqjt5ek7z0fbavxxc&ep=v1_gifs_search&rid=giphy.gif&ct=g","description":"uummmm","date":"2024-06-20T10:06:36.852Z","id":"020361918299558024-1718877996853"},{"author":"dana","title":"otra","image":"https://media.giphy.com/media/hwFK2j1eRhdb907HsP/giphy.gif?cid=ecf05e47zprrxyc9uxhqp072uvzquh5chdvbr4arp89n2kzj&ep=v1_gifs_search&rid=giphy.gif&ct=g","description":"kiss","date":"2024-06-20T10:21:40.696Z","id":"10717352530499946-1718878900697"},{"author":"manzana","title":"yo corriendo","image":"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExOWF4a2RqYXM4cWNsZnc5N2E2Ymtld3dyc3hzYWZwdGpsamF3a3BnaSZlcD12MV9naWZzX3NlYXJjaCZjdD1n/26xBPapqZgN5M1KrC/giphy.gif","description":"aaahhhhhhhh","date":"2024-06-20T10:22:44.327Z","id":"8613226125715236-1718878964328"}]
45 changes: 1 addition & 44 deletions staff/eduardo-sanchez/socialcode/api/data/users.json
Original file line number Diff line number Diff line change
@@ -1,44 +1 @@
[
{
"name": "Pepito",
"surname": "Grillo",
"email": "[email protected]",
"username": "pepitogrillo",
"password": "123123123"
},
{
"name": "Peter",
"surname": "Pan",
"email": "[email protected]",
"username": "peterpan",
"password": "123123123"
},
{
"name": "Wendy",
"surname": "Darling",
"email": "[email protected]",
"username": "WendyD",
"password": "Hola1234"
},
{
"name": "manolo",
"surname": "cabezabolo",
"email": "[email protected]",
"username": "ManoloC",
"password": "Hola1234"
},
{
"name": "Pablo",
"surname": "Picasso",
"email": "[email protected]",
"username": "PabloP",
"password": "Hola1234"
},
{
"name": "Man",
"surname": "Zana",
"email": "[email protected]",
"username": "manzana",
"password": "123123123"
}
]
[{"name":"Pepito","surname":"Grillo","email":"[email protected]","username":"pepitogrillo","password":"123123123"},{"name":"Peter","surname":"Pan","email":"[email protected]","username":"peterpan","password":"123123123"},{"name":"Wendy","surname":"Darling","email":"[email protected]","username":"WendyD","password":"Hola1234"},{"name":"manolo","surname":"cabezabolo","email":"[email protected]","username":"ManoloC","password":"Hola1234"},{"name":"Pablo","surname":"Picasso","email":"[email protected]","username":"PabloP","password":"Hola1234"},{"name":"Man","surname":"Zana","email":"[email protected]","username":"manzana","password":"123123123"},{"name":"Da","surname":"Na","email":"[email protected]","username":"dana","password":"Hola1234"}]
38 changes: 29 additions & 9 deletions staff/eduardo-sanchez/socialcode/api/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import express from 'express'
import logic from './logic/index.js'
import cors from 'cors'
import jwt, { JsonWebTokenError } from 'jsonwebtoken'
// import jwt, {JsonWebTokenError} from 'jsonwebtoken'
import jwt from 'jsonwebtoken'

import { SystemError } from './errors.js'

const { JsonWebTokenError, TokenExpiredError } = jwt

const api = express()

api.use(express.static('public'))
Expand Down Expand Up @@ -71,13 +75,20 @@ api.get('/users/:targetUsername', (req, res) => {
res.json(name)
})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

api.get('/posts', (req, res) => {
try {
logic.getAllPosts((error, posts) => {
const token = req.headers.authorization.slice(7)

const { sub: username } = jwt.verify(token, 'ManoloC & PedroP are friends')

logic.getAllPosts(username, (error, posts) => {
if (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })

Expand All @@ -87,7 +98,10 @@ api.get('/posts', (req, res) => {
res.json(posts)
})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

Expand All @@ -110,19 +124,22 @@ api.post('/posts', jsonBodyParser, (req, res) => {
res.status(201).send()
})
} catch (error) {
if (error instanceof JsonWebTokenError)
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

api.delete('/posts/:postId', (req, res) => {
const username = req.headers.authorization.slice(6)

const { postId } = req.params

try {
const token = req.headers.authorization.slice(7)

const { sub: username } = jwt.verify(token, 'ManoloC & PedroP are friends')

const { postId } = req.params

logic.deletePost(username, postId, error => {
if (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
Expand All @@ -133,7 +150,10 @@ api.delete('/posts/:postId', (req, res) => {
res.status(204).send()
})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

Expand Down
Loading

0 comments on commit dc3d7d0

Please sign in to comment.