-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.16 KB
/
index.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
const express = require("express")
const session = require("express-session")
const { checkLoggedIn, bypassLogin } = require("./middleware")
//Initialize express
const app = express()
//Configure ejs view engine
app.set("view engine", "ejs")
app.use(express.urlencoded({ extended: false }))
app.use(session({
secret: "my_session_secret",
resave: true,
saveUninitialized: false,
name: "Pat",
cookie: {
maxAge: 10000
}
}))
app.use((req, res, next) => {
res.locals.user = req.session.user
next()
})
//Configure routes
app.get("/", checkLoggedIn, (req, res) => {
res.render("home")
})
app.get("/login", bypassLogin, (req, res) => {
res.render("login", { error: null })
})
app.post("/login", (req, res) => {
if(req.body.username === "John" && req.body.password === "123") {
//create session nad store user logged details
req.session.user = { id: 1, username: "John", name: "John Doe"}
res.redirect("/")
}else {
res.render("login", { error: "Wrong credentials"})
}
})
app.get("/logout", (req, res) => {
req.session.destroy()
res.clearCookie("Pat")
res.redirect("/")
})
app.listen(3000, () => console.log("Server started on port 3000"))