-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
184 lines (168 loc) · 4.47 KB
/
app.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
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
const express = require("express");
const mongoose = require("mongoose");
const keys = require("./keys");
const app = express();
// const authe
const expressLayout = require("express-ejs-layouts");
// const passportLocalMongoose = require("passport-local-mongoose");
// const LocalStrategy = require("passport-local");
// const passport = require("passport");
const restRouter = require("./routes/rest");
const User = require("./models/User");
const Order = require("./models/Order");
const bodyParser = require("body-parser");
const session = require("express-session");
const passport = require("passport");
const passportLocal = require("./config/passport-local-strategy");
// const passportLocal = require("./config/passport-local-strategy");
const Authenticator = require("./config/authenticator");
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static("./public"));
app.set("layout extractStyles", true);
app.set("layout extractScripts", true);
// // app.use(expressLayout);
app.set("view engine", "ejs");
app.use(
session({
name: "codeialdwqd",
secret: "blashwfejbfwejf",
saveUninitialized: false,
resave: false
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
if (req.isAuthenticated()) {
// console.log(req.user);
res.locals.user = req.user;
}
next();
});
app.use("/rest", restRouter);
// app.use(
// require("express-session")({
// secret: "hey there",
// resave: false,
// saveUninitialized: false
// })
// );
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// app.use(passport.initialize());
// app.use(session());
// passport.use(new LocalStrategy(User.authenticate()));
// passport.serializeUser(User.serializeUser());
// passport.deserializeUser(User.deserializeUser());
// app.post(
// "/rest/signin",
// passport.authenticate("local", {
// successRedirect: "/rest/dashboard",
// failureRedirect: "/rest/register"
// }),
// (req, res) => {}
// );
app.get("/logout", (req,res)=>{
req.logout();
res.redirect("/rest/register")
})
app.get("/products", (req,res)=>{
res.render("products")
})
app.get("/unsubscribe", (req,res)=>{
User.findOneAndRemove({_id: req.user._id}, (err)=>{
if(err){
console.log(err);
res.redirect("/rest/register")
}else{
res.redirect("/");
}
})
})
app.post("/order", (req, res) => {
User.findById(req.user.id, (err, user) => {
// console.log(user);
if (user) {
Order.create({
quantity: req.body.quantity,
credit: req.body.quantity * 10,
user: req.user
})
.then(order => {
console.log(req.body);
user.date = new Date().getTime() + 604800000;
user.isActive = true;
user.orders.push(order);
user.save().then(()=>{
res.redirect("/rest/dashboard");
});
})
.catch(err => {
console.log(err);
});
}
});
});
app.get("/admin", Authenticator.checkAdmin, (req, res) => {
Order.find({ isActive: true })
.populate("user")
.exec((err, orders) => {
console.log(orders);
res.render("admin", { orders });
});
});
app.get("/logout", (req, res) => {
req.logOut();
res.redirect("/rest/register");
});
app.get("/order/:id", (req, res) => {
Order.findById(req.params.id, (err, order) => {
if (err) {
console.log(err);
return;
} else {
if (order) {
User.findById(order.user, (err, user) => {
if (err) {
console.log(err);
return res.redirect("/");
}
user.credit = user.credit + order.credit;
user.isActive = false;
order.isActive = false;
user.save().then(() => {
order.save().then(() => {
return res.redirect("/admin");
});
});
});
} else {
console.log("NO order found");
res.redirect("/");
}
}
});
});
app.get("/rest/unsubscribe", (req, res) => {
User.findById(req.user, (err, user) => {
if (user) {
user.remove();
return res.redirect("/rest/register");
} else {
return res.redirect("/rest/dashboard");
}
});
});
app.use("/", (req, res) => {
return res.render("index");
});
mongoose.connect(
keys.MONGOURI,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => {
app.listen(keys.PORT, () => {
console.log(`listening to port:${keys.PORT}`);
});
}
);