-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
308 lines (236 loc) · 8.81 KB
/
server.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
require("dotenv").config()
const jwt = require("jsonwebtoken")
const marked = require("marked")
const sanitizeHTML = require("sanitize-html")
const bcrypt = require("bcrypt")
const cookieParser = require("cookie-parser")
const express = require("express")
const db = require("better-sqlite3")("ourApp.db")
db.pragma("journal_mode = WAL")
// database setup here
const createTables = db.transaction(() => {
db.prepare(
`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username STRING NOT NULL UNIQUE,
password STRING NOT NULL
)
`
).run()
db.prepare(
`
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
createdDate TEXT,
title STRING NOT NULL,
body TEXT NOT NULL,
authorid INTEGER,
FOREIGN KEY (authorid) REFERENCES users (id)
)
`
).run()
})
createTables()
// database setup ends here
const app = express()
app.set("view engine", "ejs")
app.use(express.urlencoded({ extended: false }))
app.use(express.static("public"))
app.use(cookieParser())
app.use(function (req, res, next) {
// make our markdown function available
res.locals.filterUserHTML = function (content) {
return sanitizeHTML(marked.parse(content), {
allowedTags: ["p", "br", "ul", "li", "ol", "strong", "bold", "i", "em", "h1", "h2", "h3", "h4", "h5", "h6"],
allowedAttributes: {}
})
}
res.locals.errors = []
// try to decode incoming cookie
try {
const decoded = jwt.verify(req.cookies.ourSimpleApp, process.env.JWTSECRET)
req.user = decoded
} catch (err) {
req.user = false
}
res.locals.user = req.user
console.log(req.user)
next()
})
app.get("/", (req, res) => {
if (req.user) {
const postsStatement = db.prepare("SELECT * FROM posts WHERE authorid = ? ORDER BY createdDate DESC")
const posts = postsStatement.all(req.user.userid)
return res.render("dashboard", { posts })
}
res.render("homepage")
})
app.get("/login", (req, res) => {
res.render("login")
})
app.get("/logout", (req, res) => {
res.clearCookie("ourSimpleApp")
res.redirect("/")
})
app.post("/login", (req, res) => {
let errors = []
if (typeof req.body.username !== "string") req.body.username = ""
if (typeof req.body.password !== "string") req.body.password = ""
if (req.body.username.trim() == "") errors = ["Invalid username / password."]
if (req.body.password == "") errors = ["Invalid username / password."]
if (errors.length) {
return res.render("login", { errors })
}
const userInQuestionStatement = db.prepare("SELECT * FROM users WHERE USERNAME = ?")
const userInQuestion = userInQuestionStatement.get(req.body.username)
if (!userInQuestion) {
errors = ["Invalid username / password."]
return res.render("login", { errors })
}
const matchOrNot = bcrypt.compareSync(req.body.password, userInQuestion.password)
if (!matchOrNot) {
errors = ["Invalid username / password."]
return res.render("login", { errors })
}
const ourTokenValue = jwt.sign(
{ exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24, skyColor: "blue", userid: userInQuestion.id, username: userInQuestion.username },
process.env.JWTSECRET
)
res.cookie("ourSimpleApp", ourTokenValue, {
httpOnly: true,
secure: true,
sameSite: "strict",
maxAge: 1000 * 60 * 60 * 24
})
res.redirect("/")
})
function mustBeLoggedIn(req, res, next) {
if (req.user) {
return next()
}
return res.redirect("/")
}
app.get("/create-post", mustBeLoggedIn, (req, res) => {
res.render("create-post")
})
function sharedPostValidation(req) {
const errors = []
if (typeof req.body.title !== "string") req.body.title = ""
if (typeof req.body.body !== "string") req.body.body = ""
// trim - sanitize or strip out html
req.body.title = sanitizeHTML(req.body.title.trim(), { allowedTags: [], allowedAttributes: {} })
req.body.body = sanitizeHTML(req.body.body.trim(), { allowedTags: [], allowedAttributes: {} })
if (!req.body.title) errors.push("You must provide a title.")
if (!req.body.body) errors.push("You must provide content.")
return errors
}
app.get("/edit-post/:id", mustBeLoggedIn, (req, res) => {
// try to look up the post in question
const statement = db.prepare("SELECT * FROM posts WHERE id = ?")
const post = statement.get(req.params.id)
if (!post) {
return res.redirect("/")
}
// if you're not the author, redirect to homepage
if (post.authorid !== req.user.userid) {
return res.redirect("/")
}
// otherwise, render the edit post template
res.render("edit-post", { post })
})
app.post("/edit-post/:id", mustBeLoggedIn, (req, res) => {
// try to look up the post in question
const statement = db.prepare("SELECT * FROM posts WHERE id = ?")
const post = statement.get(req.params.id)
if (!post) {
return res.redirect("/")
}
// if you're not the author, redirect to homepage
if (post.authorid !== req.user.userid) {
return res.redirect("/")
}
const errors = sharedPostValidation(req)
if (errors.length) {
return res.render("edit-post", { errors })
}
const updateStatement = db.prepare("UPDATE posts SET title = ?, body = ? WHERE id = ?")
updateStatement.run(req.body.title, req.body.body, req.params.id)
res.redirect(`/post/${req.params.id}`)
})
app.post("/delete-post/:id", mustBeLoggedIn, (req, res) => {
// try to look up the post in question
const statement = db.prepare("SELECT * FROM posts WHERE id = ?")
const post = statement.get(req.params.id)
if (!post) {
return res.redirect("/")
}
// if you're not the author, redirect to homepage
if (post.authorid !== req.user.userid) {
return res.redirect("/")
}
const deleteStatement = db.prepare("DELETE FROM posts WHERE id = ?")
deleteStatement.run(req.params.id)
res.redirect("/")
})
app.get("/post/:id", (req, res) => {
const statement = db.prepare("SELECT posts.*, users.username FROM posts INNER JOIN users ON posts.authorid = users.id WHERE posts.id = ?")
const post = statement.get(req.params.id)
if (!post) {
return res.redirect("/")
}
const isAuthor = post.authorid === req.user.userid
res.render("single-post", { post, isAuthor })
})
app.post("/create-post", mustBeLoggedIn, (req, res) => {
const errors = sharedPostValidation(req)
if (errors.length) {
return res.render("create-post", { errors })
}
// save into database
const ourStatement = db.prepare("INSERT INTO posts (title, body, authorid, createdDate) VALUES (?, ?, ?, ?)")
const result = ourStatement.run(req.body.title, req.body.body, req.user.userid, new Date().toISOString())
const getPostStatement = db.prepare("SELECT * FROM posts WHERE ROWID = ?")
const realPost = getPostStatement.get(result.lastInsertRowid)
res.redirect(`/post/${realPost.id}`)
})
app.post("/register", (req, res) => {
const errors = []
if (typeof req.body.username !== "string") req.body.username = ""
if (typeof req.body.password !== "string") req.body.password = ""
req.body.username = req.body.username.trim()
if (!req.body.username) errors.push("You must provide a username.")
if (req.body.username && req.body.username.length < 3) errors.push("Username must be at least 3 characters.")
if (req.body.username && req.body.username.length > 10) errors.push("Username cannot exceed 10 characters.")
if (req.body.username && !req.body.username.match(/^[a-zA-Z0-9]+$/)) errors.push("Username can only contain letters and numbers.")
// check if username exists already
const usernameStatement = db.prepare("SELECT * FROM users WHERE username = ?")
const usernameCheck = usernameStatement.get(req.body.username)
if (usernameCheck) errors.push("That username is already taken.")
if (!req.body.password) errors.push("You must provide a password.")
if (req.body.password && req.body.password.length < 10) errors.push("Password must be at least 10 characters.")
if (req.body.password && req.body.password.length > 70) errors.push("Password cannot exceed 70 characters.")
if (errors.length) {
return res.render("homepage", { errors })
}
// save the new user into a database
const salt = bcrypt.genSaltSync(10)
req.body.password = bcrypt.hashSync(req.body.password, salt)
const ourStatement = db.prepare("INSERT INTO users (username, password) VALUES (?, ?)")
const result = ourStatement.run(req.body.username, req.body.password)
const lookupStatement = db.prepare("SELECT * FROM users WHERE ROWID = ?")
const ourUser = lookupStatement.get(result.lastInsertRowid)
// log the user in by giving them a cookie
const ourTokenValue = jwt.sign(
{ exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24, skyColor: "blue", userid: ourUser.id, username: ourUser.username },
process.env.JWTSECRET
)
res.cookie("ourSimpleApp", ourTokenValue, {
httpOnly: true,
secure: true,
sameSite: "strict",
maxAge: 1000 * 60 * 60 * 24
})
res.redirect("/")
})
app.listen(3000)