forked from LukasSkywalker/m307-demo2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
78 lines (66 loc) · 1.9 KB
/
config.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
import express from "express";
import { engine } from "express-handlebars";
import pg from "pg";
const { Pool } = pg;
import cookieParser from "cookie-parser";
import multer from "multer";
const upload = multer({ dest: "public/uploads/" });
import sessions from "express-session";
import bcrypt from "bcrypt";
export function createApp(dbconfig) {
const app = express();
const pool = new Pool(dbconfig);
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
app.set("views", "./views");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
sessions({
secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
saveUninitialized: true,
cookie: { maxAge: 86400000, secure: false },
resave: false,
})
);
app.locals.pool = pool;
app.get("/register", function (req, res) {
res.render("register");
});
app.post("/register", function (req, res) {
var password = bcrypt.hashSync(req.body.password, 10);
pool.query(
"INSERT INTO users (name, birthdate, loginname, password) VALUES ($1, $2, $3, $4)",
[req.body.name, req.body.birthdate, req.body.loginname, password],
(error, result) => {
if (error) {
console.log(error);
}
res.redirect("/login");
}
);
});
app.get("/login", function (req, res) {
res.render("login");
});
app.post("/login", function (req, res) {
pool.query(
"SELECT * FROM users WHERE loginname = $1",
[req.body.loginname],
(error, result) => {
if (error) {
console.log(error);
}
if (bcrypt.compareSync(req.body.password, result.rows[0].password)) {
req.session.userid = result.rows[0].id;
res.redirect("/");
} else {
res.redirect("/login");
}
}
);
});
return app;
}
export { upload };