Skip to content

Commit

Permalink
add admin support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmnouira committed Dec 4, 2019
1 parent ea45cf8 commit 6dc24a4
Show file tree
Hide file tree
Showing 23 changed files with 1,319 additions and 32 deletions.
43 changes: 43 additions & 0 deletions authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* REST web service authentication */

const jwt = require('jsonwebtoken')

const APP_SECRET = "myappsecret";
const USERNAME ="admin";
const PASSWORD = "secret";

module.exports = (req, res, next) => {
if((req.url== "/api/login" || req.url =="/login") && req.method == "POST") {
if(req.body != null && req.body.name == USERNAME && req.body.password == PASSWORD) {
let token = jwt.sign({data: USERNAME, expresIn: "1h"}, APP_SECRET);
res.json({success: true, token: token});
} else {
res.json({success: false});
}
res.end();
return;

} else if ((((req.url.startsWith("api/products")
|| req.url.startsWith("/products"))
|| (req.url.startsWith("/api/categories")
|| req.url.startsWith("/categories"))) && req.method != "GET")
|| ((req.url.startsWith("/api/orders")
|| req.url.startsWith("/orders")) && req.method != "POST" )) {

let token = req.headers["authorization"];
if(token != null && token.startsWith("Bearer<")) {
token = token.substring(7, token.length -1 );
try {
jwt.verify(token, APP_SECRET);
next();
return;

} catch(err) {}

}
res.statusCode = 401;
res.end();
return;
}
next();
}
31 changes: 31 additions & 0 deletions data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = () => {

return {
products: [
{ id: 1, name: "Kayak", category: "Watersports", description: "A boat for one person", price: 275, image: 'kayak.jpg' },

{ id: 2, name: "Lifejacket", category: "Watersports",
description: "Protective and fashionable", price: 48.95, image: 'life-jack.jpg' },
{ id: 3, name: "Soccer Ball", category: "Soccer",
description: "FIFA-approved size and weight", price: 19.50, image: 'soccer-ball.jpg' },
{ id: 4, name: "Corner Flags", category: "Soccer",
description: "Give your playing field a professional touch",
price: 34.95, image: 'corner-flags.jpg' },
{ id: 5, name: "Stadium", category: "Soccer",
description: "Flat-packed 35,000-seat stadium", price: 79500, image: 'stadium.jpg' },
{ id: 6, name: "Thinking Cap", category: "Chess",
description: "Improve brain efficiency by 75%", price: 16, image: 'thinking-cap.jpg' },
{ id: 7, name: "Unsteady Chair", category: "Chess",
description: "Secretly give your opponent a disadvantage",
price: 29.95, image: 'unsteady-chair.jpg' },
{ id: 8, name: "Human Chess Board", category: "Chess",
description: "A fun game for the family", price: 75 },
{ id: 9, name: "Bling Bling King", category: "Chess",
description: "Gold-plated, diamond-studded King", price: 1200 }
],

orders : []

}

}
Loading

0 comments on commit 6dc24a4

Please sign in to comment.