Skip to content

Commit

Permalink
changed authentication so it work with the new front end
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefkarim committed Sep 24, 2023
1 parent 9e20e8f commit 8e0e9ae
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions controllers/indexController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const collectionModel = require('../models/collection')

// Home page
exports.index = asyncHandler(async(req,res,next)=>{
console.log(req.user)


res.render('index',{title:'E-commerce', user: req.user })
res.send({username: req.user.username })


})
Expand Down
6 changes: 6 additions & 0 deletions controllers/itemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ exports.create_post_api = [


})]
//handleing creating item GET API
exports.create_get_api =asyncHandler(async(req,res,next)=>{
const collections = await collectionModel.find({})
res.send({title:"collections create",collections:collections})

})

//handling deleting a item
exports.delete = asyncHandler(async(req,res,next)=>{
Expand Down
4 changes: 2 additions & 2 deletions helpers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module.exports=function authenticate(req, res, next) {
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) => {
if (err || user.username !== "admin") {
console.log("eror", err);
return res.sendStatus(403);
return res.status(403).send({msg:'Forbidden',status:403});
}
req.user = user;
next();
});
} else {
return res.sendStatus(403).send({errors:'Forrbiden'});
return res.status(403).send([{msg:'Forbidden',status:403}]);
}
}
8 changes: 1 addition & 7 deletions request.rest
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
POST http://localhost:3000/item/create/api
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNjk0NDEzMzU0fQ.-bg_1nIVgNkEmWLDEivnXna4Lx0aT4qiOVSTSXCM_Gg
content-type: application/json

{
"name":"karim"
}
GET http://localhost:3000/collection/create/api
4 changes: 3 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const express = require("express");
const router = express.Router();
const index_controller = require('../controllers/indexController')
const authenticate = require('../helpers/auth')

/* GET home page. */
router.get("/",index_controller.index);
router.post("/",authenticate,index_controller.index);

//API Home page

Expand Down
2 changes: 1 addition & 1 deletion routes/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ router.get('/create',authenticate,item_controller.edit_get)
router.post('/create',authenticate,upload.array('src',10),item_controller.edit_post)

//creating item
router.get('/create/api',authenticate,item_controller.edit_get_api)
router.get('/create/api',authenticate,item_controller.create_get_api)
router.post('/create/api',authenticate,upload.array('src',10),item_controller.create_post_api)

//editing item
Expand Down

0 comments on commit 8e0e9ae

Please sign in to comment.