-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (41 loc) · 1.18 KB
/
Copy pathapp.js
File metadata and controls
52 lines (41 loc) · 1.18 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
const path = require("path")
const express = require("express")
const session = require("express-session")
const mongoStore = require("connect-mongodb-session")
const db = require("./data/database")
const demoRoutes = require("./routes/demo")
const mongoSessionStore = mongoStore(session)
const app = express()
const sessionStore = new mongoSessionStore({
uri: "mongodb://127.0.0.1:27017",
databaseName: "auth-demo",
collection: "sessions"
})
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.use(express.static("public"))
app.use(express.urlencoded({ extended: false }))
app.use(session({
secret: "sessionSecret",
resave: false,
saveUninitialized: false,
store: sessionStore
}))
app.use(async function(req, res, next) {
const thisUser = req.session.user
if(!thisUser){
return next()
}
const matchUser = await db.getDb().collection("users").findOne({_id: thisUser.id})
const userIsAdmin = matchUser.isAdmin
res.locals.user = thisUser
res.locals.isAdmin = userIsAdmin
next()
})
app.use(demoRoutes)
app.use(function(error, req, res, next) {
res.render("500")
})
db.connectToDatabase().then(function () {
app.listen(3000)
})