-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (111 loc) · 2.72 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
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
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const mustacheExpress = require("mustache-express");
const { v4: uuidv4 } = require("uuid");
const app = express();
const environment = app.get("env");
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static("public"));
app.engine("mustache", mustacheExpress());
app.set("view engine", "mustache");
app.set("views", __dirname + "/views");
let users = [
{
name: "alice",
password: "password123",
balance: 60250,
},
{
name: "eve",
password: "123456",
balance: 20,
},
];
let sessions = {};
const isLoggedIn = (req) => req.cookies["sessionId"] !== undefined;
const login = (res, user) => {
let sessionId = uuidv4();
sessions[sessionId] = user;
res.cookie("sessionId", sessionId, {
httpOnly: true,
sameSite: "strict",
});
};
app.get("/", (req, res) => {
const source = req.query.source;
const title = source
? `Welcome ${source} member`
: "Welcome to the insecure bank";
res.render("welcome", {
title,
loggedIn: isLoggedIn(req),
});
});
app.get("/login", (req, res) => {
if (!isLoggedIn(req)) {
res.render("login");
} else {
res.redirect("/transfer");
}
});
app.post("/login", (req, res) => {
const { name, password } = req.body;
let user = users.find((u) => u.name === name && u.password === password);
if (!user) {
res.send("Invalid username or password");
return;
}
login(res, user);
res.redirect("/transfer");
});
app.get("/logout", (req, res) => {
res.clearCookie("sessionId");
res.redirect("/login");
});
app.get("/transfer", (req, res) => {
let sessionId = req.cookies["sessionId"];
let user = sessions[sessionId];
if (!user) {
res.clearCookie("sessionId");
res.redirect("/login");
return;
}
res.render("transfer", {
name: user.name,
balance: user.balance,
});
});
app.post("/transfer", (req, res) => {
let sessionId = req.cookies["sessionId"];
let user = sessions[sessionId];
if (!user) {
res.send("Not logged in");
return;
}
const { amount, to } = req.body;
let recipient = users.find((u) => u.name === to);
recipient.balance += new Number(amount);
user.balance -= new Number(amount);
res.redirect("/transfer");
});
app.get("/register", (req, res) => {
res.render("register");
});
app.post("/register", (req, res) => {
const { name, password } = req.body;
let user = {
name,
password,
balance: 100, // free money!
};
users.push(user);
login(res, user);
res.redirect("/transfer");
});
if (environment === "development") {
app.listen(4000, "127.0.0.1");
} else {
app.listen(80, "0.0.0.0");
}