This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
64 lines (57 loc) · 1.91 KB
/
routes.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
"use strict";
const fs = require("fs");
const sha1 = require("sha1");
const express = require("express");
const router = express.Router();
const path = require("path");
const passport = require("passport");
const {
ensureAuthenticated, forwardAuthenticated
} = module.exports = {
ensureAuthenticated: (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/");
},
forwardAuthenticated: (req, res, next) => {
if (!req.isAuthenticated()) {
return next();
}
res.redirect("/chat");
}
};
router.get("/", forwardAuthenticated, (req, res) => res.sendFile(path.join(__dirname, "pages/login.html")));
router.get("/registration", forwardAuthenticated, (req, res) => res.sendFile(path.join(__dirname, "pages/registration.html")));
router.post("/registration", (req, res) => {
let users = JSON.parse(fs.readFileSync("data/users.json", "utf-8"));
if (users[req.body.email] !== undefined) {
res.send("Пользователь с указанным email уже зарегистрирован.");
} else if (!/^\w{6,20}$/.test(req.body.nick)) {
res.send("Wrong nick.");
} else if (!/^\w{6,20}$/.test(req.body.password1)) {
res.send("Wrong password.");
} else if (req.body.password1 !== req.body.password2) {
res.send("Passwords doesn't match.");
} else {
users[req.body.email] = {
"nick": req.body.nick,
"password": sha1(req.body.password1),
"rooms": []
};
fs.writeFileSync("data/users.json", JSON.stringify(users));
res.redirect("/chat");
}
});
router.post("/login", forwardAuthenticated, (req, res, next) => passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/?authenticationError=1"
})(req, res, next));
router.get("/chat", ensureAuthenticated, (req, res) => res.sendFile(path.join(__dirname, "pages/chat.html"), {
user: req.user
}));
router.get("/logout", ensureAuthenticated, (req, res) => {
req.logout();
res.redirect("/");
});
module.exports = router;