forked from frenvi-benjamin/instagram_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
173 lines (132 loc) · 4.89 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
require("dotenv").config()
const express = require("express")
const app = express()
const path = require("path")
// add body parser to read post body
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// mongoose import and settings
const mongoose = require("mongoose")
mongoose.set("returnOriginal", false)
mongoose.set("debug", true)
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
// DB models
const User = require("./models/userModel")
// ensure eatlery account is there
User.findOne({ instagramUserID: "17841428462715235" })
.then(user => {
if (!user) throw Error("Application needs the eatlery account in DB to run properly")
})
// ensure example account is there
User.findOne({ instagramUserID: "17841447654588327" })
.then(user => {
if (!user) throw Error("Application needs the eatlery account in DB to run properly")
})
// session
const session = require("express-session")
// use mongodb as session store
const MongoStore = require("connect-mongo")
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_CONNECTION_URL,
}),
cookie: { maxAge: 1000 * 60 * 60 * 24 * 14 } // 14 days
})
)
// set view engine to ejs
app.set("view engine", "ejs")
// set all paths
app.use("/img", express.static(path.join(__dirname, "/static/img")))
app.use("/js", express.static(path.join(__dirname, "/static/js")))
app.use("/css", express.static(path.join(__dirname, "/static/css")))
app.use("/qr-scanner", express.static(path.join(__dirname, "/node_modules/qr-scanner")))
app.use("/bootstrap", express.static(path.join(__dirname, "/node_modules/bootstrap/dist")))
app.use("/bootstrap-social", express.static(path.join(__dirname, "/node_modules/bootstrap-social")))
app.use("/jquery", express.static(path.join(__dirname, "/node_modules/jquery/dist")))
app.use("/fontawesome", express.static(path.join(__dirname, "/node_modules/@fortawesome/fontawesome-free")))
const startServer = app.listen(process.env.PORT, () => {
console.log(`App listening at ${process.env.HOST}:${process.env.PORT}`)
})
// connect to database
mongoose.connect(process.env.MONGODB_CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
// start server when db is connected
.then(() => startServer)
.catch((err) => console.log(err))
// helper module
const helper = require("./modules/helper")
// refresh tokens every 24.855 days (maximum)
helper.refreshAccessTokens()
setInterval(helper.refreshAccessTokens, 2147483647)
app.locals.env = process.env
const sharedController = require("./controllers/sharedController")
app.use(sharedController.auth)
app.use((req, res, next) => {
if (req.session.cookiesAccepted) {
res.locals.cookiesAccepted = true
}
else {
res.locals.cookiesAccepted = false
}
next()
})
// index
app.use("/", require("./routes/indexRoute"))
// me
app.use("/me", sharedController.checkForUserSession, require("./routes/meRoute"))
// admin
app.use("/admin", require("./routes/adminRoute"))
// campaign
app.use("/campaign", require("./routes/campaignRoute"))
//scanner
app.use("/scanner", require("./routes/scannerRoute"))
//choose-post
app.use("/choose-post", require("./routes/choosePostRoute"))
//choose-reward
app.use("/choose-reward", require("./routes/chooseRewardRoute"))
//preview
app.use("/preview", (req, res) => res.render("preview"))
// setup
app.use("/setup", require("./routes/setupRoute"))
//kontakt
app.get("/kontakt", (req, res) => res.render("kontakt"))
//datenschutz
app.get("/datenschutz", (req, res) => res.render("datenschutz"))
//impressum
app.get("/impressum", (req, res) => res.render("impressum"))
app.get("/accept-cookies", (req, res) => {
req.session.cookiesAccepted = true
res.sendStatus(200)
})
const ejs = require("ejs")
//logout
app.use("/logout", (req, res) => {
req.session.destroy()
ejs.renderFile(path.join(__dirname, "/views/logout.ejs"), { env: process.env})
.then(rendered => res.send(rendered))
})
const base64url = require("base64url")
const Session = require("./models/sessionModel")
// deauthorization (instagram)
app.use("/deauthorize", (req, res) => {
res.sendStatus(200)
})
// data deletion (instagram)
app.use("/delete-data", (req, res) => {
const encodedPayload = req.body.signed_request.split(".")[1]
const decodedPayload = base64url.decode(encodedPayload)
const parsedPayload = JSON.parse(decodedPayload)
Promise.all([
User.findOneAndDelete({ instagramUserID: parsedPayload.user_id }),
Session.deleteMany({ session: { $regex: parsedPayload.user_id, $options: "i" } })
])
.then(() => res.sendStatus(200))
})
app.use("/close-account", (req, res) => res.render("close-account"))
app.use(require("./routes/404Route"))