-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
139 lines (121 loc) · 3.26 KB
/
server.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require('dotenv').config()
const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)
const jwt = require('express-jwt')
const jwksRsa = require('jwks-rsa')
const PORT = process.env.PORT || 4000
const app = express()
// Set up Auth0 configuration
const authConfig = {
domain: 'dev--vb7loon.auth0.com',
audience: 'http://localhost:4000'
}
// Define middleware that validates incoming bearer tokens
// using JWKS from dev--vb7loon.auth0.com
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`
}),
audience: authConfig.audience,
issuer: `https://${authConfig.domain}/`,
algorithm: ['RS256']
})
// Middleware
app.use(cors())
app.use(morgan('dev'))
app.use(express.json())
//Sequelize Models
const db = require('./models')
const Category = db.Category
const Product = db.Product
// Router Files
// Routes
app.get('/api/test', checkJwt, (req, res,) => {
res.json ({
message:'We got Routes'
})
// const error = new Error('Something Broke')
})
app.get('/api/categories', (req, res, next) => {
Category.findAll({
include: [{ model : Product }]
})
.then(categories => {
res.json({
categories
})
})
.catch(error => {
next(error)
})
})
app.get('/api/products', (req, res, next) => {
Product.findAll({
include: [{model: Category}]
})
.then(products => {
res.json({
products
})
})
.catch(error => {
next(error)
})
})
app.get('/api/products/:id', (req, res, next) => {
const id = req.params.id
Product.findByPk(id, {
include: [{ model: Category}]
})
.then(product => {
res.json({
product
})
})
.catch(error => {
console.log (error)
})
})
app.post('/api/checkout', async (req, res, next) => {
const lineItem = req.body
const lineItems = [lineItem]
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: lineItems,
success_url: 'http://localhost:3000/success',
cancel_url: 'http://localhost:3000/cancel'
})
res.json({ session })
}
catch (error) {
next(error)
}
})
app.get('/api/external', checkJwt, (req, res) => {
res.send({
msg: 'Your Access Token was successfully validated!'
})
})
//Error Handling
// The following 2 `app.use`'s MUST follow ALL your routes/middleware
app.use(notFound)
app.use(errorHandler)
function notFound(req, res, next) {
res.status(404).send({ error: 'Not found!', status: 404, url: req.originalUrl })
}
// eslint-disable-next-line
function errorHandler(err, req, res, next) {
console.error('ERROR', err)
const stack = process.env.NODE_ENV !== 'production' ? err.stack : undefined
res.status(500).send({ error: err.message, stack, url: req.originalUrl })
}
app.listen(PORT, () => {
// eslint-disable-next-line no-undef
console.log(`Server going deep on ${PORT}`)
})