-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (69 loc) · 2.43 KB
/
Copy pathapp.js
File metadata and controls
83 lines (69 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require('dotenv').config()
const express = require('express')
const app = express();
const port = process.env.PORT || 5000;
const jwt = require('jsonwebtoken')
const cors = require('cors')
// const methodOverride = require('method-override')
// const sequelize = require('./models/index')
const UserControllers = require('./controllers/UserController')
const ProductControllers = require('./controllers/ProductController')
const OrderControllers = require('./controllers/OrderController')
const CartControllers = require('./controllers/CartController')
app.use(express.urlencoded({
extended: true
}))
app.use(cors({
origin: '*'
}))
app.options('*', cors())
// user routes
app.post('/register', UserControllers.register)
app.post('/login', UserControllers.login)
// product routes
app.get('/product/:slug', ProductControllers.showProduct)
app.get('/products/all', ProductControllers.showAllProducts)
// superuser routes
app.post('/product/new', verifyJWT, ProductControllers.createProduct)
app.patch('/product/edit/:slug', verifyJWT, ProductControllers.editProduct)
app.get('/superuser', verifyJWT, UserControllers.checkSuperUser)
app.delete('/product/:slug', verifyJWT, ProductControllers.deleteProduct)
// cart routes
app.post('/product/:id', verifyJWT, CartControllers.addToCart)
app.post('/cart/:id', verifyJWT, CartControllers.removeFromCart)
app.get('/cart', verifyJWT, CartControllers.showCart)
// order routes
app.listen(process.env.PORT || port, () => {
console.log(`Shopping App listening on port: ${port}`)
})
function verifyJWT(req, res, next) {
// get the jwt token from the request header
const authToken = req.headers.auth_token
console.log(authToken)
// console.log(req.headers) // added this line and it worked?
// check if authToken header value is empty, return err if empty
if (!authToken) {
res.json({
success: false,
message: "Auth header value is missing"
})
return
}
// verify that JWT is valid and not expired
try {
// if verify success, proceed
const userData = jwt.verify(authToken, process.env.JWT_SECRET, {
algorithms: ['HS384']
})
// store jwt token into res.locals.jwtData
res.locals.jwtData = userData;
next()
} catch (err) {
// if fail, return error msg
res.json({
success: false,
message: "Auth token is invalid"
})
return
}
}