-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
192 lines (170 loc) · 5.04 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
185
186
187
188
189
190
191
192
const express = require("express");
const path = require("path");
//set up require the database to store and retieve users, passwords
const redis = require("redis");
const session = require("express-session");
const bcrypt = require("bcrypt");
const { formatDistance } = require("date-fns");
const client = redis.createClient();
const { promisify } = require("util");
const app = express();
const RedisStore = require("connect-redis")(session);
//Now add some middleware to process the url encoded data
app.use(express.urlencoded({ extended: true }));
app.use(
session({
store: new RedisStore({ client: client }),
resave: true,
cookie: {
maxAge: 3600000, //10 hours
httpOnly: false,
secure: false,
sameSite: "lax",
},
secret: "AsecretPWtoValidateAsession0k?",
saveUninitialized: true,
})
);
app.set("view engine", "pug");
app.set("views", path.join(__dirname, "views"));
const ahget = promisify(client.hget).bind(client);
const asmembers = promisify(client.smembers).bind(client);
const ahkeys = promisify(client.hkeys).bind(client);
const aincr = promisify(client.incr).bind(client);
const alrange = promisify(client.lrange).bind(client);
//Tell the program to watch the root folder for an index page and listen to port 3005
app.get("/", async (req, res) => {
if (req.session.userid) {
const currentUserName = await ahget(
`user:${req.session.userid}`,
"username"
);
const following = await asmembers(`following:${currentUserName}`);
const users = await ahkeys("users");
const timeline = [];
const posts = await alrange(`timeline:${currentUserName}`, 0, 100);
for (post of posts) {
const timestamp = await ahget(`post:${post}`, "timestamp");
const timeString = formatDistance(
new Date(),
new Date(parseInt(timestamp))
);
timeline.push({
message: await ahget(`post:${post}`, "message"),
author: await ahget(`post:${post}`, "username"),
timeString: timeString,
});
}
res.render("dashboard", {
users: users.filter(
(user) => user !== currentUserName && following.indexOf(user) === -1
),
currentUserName,
timeline,
});
} else {
res.render("login");
}
});
app.get("/post", (req, res) => {
if (req.session.userid) {
res.render("post");
} else {
res.render("login");
}
});
app.post("/post", async (req, res) => {
if (!req.session.userid) {
res.render("login");
return;
}
const { message } = req.body;
const currentUserName = await ahget(`user:${req.session.userid}`, "username");
const postid = await aincr("postid");
client.hmset(
`post:${postid}`,
"userid",
req.session.userid,
"username",
currentUserName,
"message",
message,
"timestamp",
Date.now()
);
client.lpush(`timeline:${currentUserName}`, postid);
const followers = await asmembers(`followers:${currentUserName}`);
for (follower of followers) {
client.lpush(`timeline:${follower}`, postid);
}
res.redirect("/");
});
app.post("/follow", (req, res) => {
if (!req.session.userid) {
res.render("login");
return;
}
const { username } = req.body;
client.hget(
`user:${req.session.userid}`,
"username",
(err, currentUserName) => {
client.sadd(`following:${currentUserName}`, username);
client.sadd(`followers:${username}`, currentUserName);
}
);
res.redirect("/");
});
app.post("/", (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
res.render("error", {
message: "Please create a username AND a password.",
});
return;
}
const saveSessionAndRenderDashboard = (userid) => {
req.session.userid = userid;
req.session.save();
client.hkeys("users", (err, users) => {
res.render("dashboard", {
users,
});
});
};
// console.log(req.body, username, password);
//HGET is a redis command that returns the value of a field in a hash
const handleSignup = (username, password) => {
//user doesn't exist, so a signup process begins
client.incr("userid", async (err, userid) => {
client.hset("users", username, userid);
const saltRounds = 10;
const hash = await bcrypt.hash(password, saltRounds);
client.hset(`user:${userid}`, "hash", hash, "username", username);
saveSessionAndRenderDashboard(userid);
});
};
//here comes the login
const handleLogin = (userid, password) => {
client.hget(`user:${userid}`, "hash", async (err, hash) => {
const result = await bcrypt.compare(password, hash); //returns true or false
if (result) {
saveSessionAndRenderDashboard(userid);
//then the PS is matching
} else {
res.render("error", {
message: "Password is incorrect",
});
return;
}
});
};
client.hget("users", username, (err, userid) => {
if (!userid) {
handleSignup(username, password);
} else {
handleLogin(userid, password);
}
});
});
app.listen(3005, () => console.log("Server is ready Sir!"));