-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,319 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : [] | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.