-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
186 lines (153 loc) · 4.51 KB
/
Copy pathapp.js
File metadata and controls
186 lines (153 loc) · 4.51 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const express = require('express')
const passport = require('passport')
const cookieSession = require('cookie-session')
require('./passport')
const db = require('./lib/db')
// const validate = require('./lib/validate')
const app = express()
// serve files out of the public directory
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
const port = 9999
//Cookie Sessions
app.use(cookieSession({
name: 'musicmonk-session',
keys: ['key1', 'key2']
}))
//Check if User is logged in
const isLoggedIn = (req, res, next) => {
if (req.user){
next()
}
else {
res.sendStatus(401)
}
}
//Google Authentication functions
app.use(passport.initialize());
app.use(passport.session());
// set the template engine
app.set('view engine', 'hbs')
app.get('/logged_out', (req, res)=>{
res.send('You are not logged in!')
})
//Google Route Failure
app.get('/failed', (req, res)=> res.send('Login Failed'))
app.get('/success', isLoggedIn, (req, res)=> {
db.getCategoryList()
.then((lists)=>{
res.render('index', {lists: lists})
})
})
// Google Authentication
app.get('/google',
passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/google/callback',
passport.authenticate('google', { failureRedirect: '/failed' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/success')
})
// const isAuthenticated = function authenticate () {
// if (passport.authenticate.user.id == '') {
// console.log('#@#@##@ I GUESSSS IT WORKED @##$#@#@#$')
// } else (
// console.log('!!!!! GUESS IT DIDNT WORK !!!!')
// )
// }
app.get('/logout', (req, res)=>{
req.session = null
req.logout()
res.redirect('/logged_out')
})
// // Shows the lists on the homepage
app.get('/', isLoggedIn, function (req, res) {
db.getCategoryList()
.then((lists)=>{
res.render('index', {lists: lists})
})
})
app.param('category_id', function (req, res, nextFn, category_id) {
const getProductsPromise = db.getProducts(category_id)
const getCategoryPromise = db.getCategory(category_id)
Promise.all([getProductsPromise, getCategoryPromise])
.then(([products, category]) => {
req.monkMusic = req.monkMusic || {}
req.monkMusic.products = products
req.monkMusic.category = category
console.log('******* THIS IS THE PRODUCTS *******')
console.log(category, '*****HEEEEYYYYYYYYYYYYYYY')
nextFn()
})
.catch((err)=> {
console.log('AARHHHHHH DIDNT WORK', err)
res.status(404).render('error_page')
})
})
app.get('/category/:category_id', isLoggedIn, function (req, res) {
const theProducts = req.monkMusic.products
const productCategoryTitle = req.monkMusic.category[0]['category_name']
console.log(productCategoryTitle, '**#*#*#*#*#*#* CHECK THIS OUT!!')
db.getCategoryList()
.then((lists)=>{
res.render('products_page', {theProducts:theProducts, categoryTitle: productCategoryTitle})
})
})
app.param('products_id', function (req, res, nextFn, products_id) {
db.getItem(products_id)
.then((item)=> {
req.monkMusic = req.monkMusic || {}
req.monkMusic.item = item
console.log('******* THIS IS THE PRODUCTS *******')
console.log(item, '*****HEEEEYYYYYYYYYYYYYYY')
nextFn()
})
.catch((err)=> {
console.log('AARHHHHHH DIDNT WORK', err)
res.status(404).send('error_page')
})
})
app.get('/category/:category_id/:products_id', isLoggedIn,function (req, res) {
const theItem = req.monkMusic.item
console.log(theItem, '**#*#*#*#*#*#* getItem PROMISE')
db.getProductsList()
.then((result)=>{
res.render('item_page', {theItem:theItem})
})
.catch((err)=>{
console.log('bannnaaanna')
res.status(404).send('error_page')
})
})
const startExpressApp = () => {
app.listen(port, () => {
console.log('express is listening on port ' + port)
})
}
function bootupSequenceFailed (err) {
console.error ('Uh Ohh... could not connect to the database!:', err)
console.error ('Goodbye!')
process.exit(1)
}
function fetchCategoryList () {
db.getCategoryList()
.then((lists)=>{
// console.log ('the lists:')
// console.log (lists)
})
}
function fetchProductsList () {
db.getProductsList()
.then((products)=>{
console.log (products)
})
}
// Global kickoff point
db.connect()
.then(startExpressApp)
.then(fetchProductsList)
.then(fetchCategoryList)
.then(()=> {
console.log ('You connected to the database!')
})
.catch(bootupSequenceFailed)