diff --git a/pages/home.html b/pages/home.html new file mode 100644 index 0000000..a51ffbb --- /dev/null +++ b/pages/home.html @@ -0,0 +1,16 @@ + + + + + Welcome Home + + + +
+ +
+ + diff --git a/pages/logout.html b/pages/logout.html new file mode 100644 index 0000000..51e1cee --- /dev/null +++ b/pages/logout.html @@ -0,0 +1,15 @@ + + + + + Logged out. + + + +
+ +
+ + diff --git a/pages/notlogged.html b/pages/notlogged.html new file mode 100644 index 0000000..994787d --- /dev/null +++ b/pages/notlogged.html @@ -0,0 +1,15 @@ + + + + + Please Log In First. + + + +
+ +
+ + diff --git a/pages/wronginfo.html b/pages/wronginfo.html new file mode 100644 index 0000000..d25c372 --- /dev/null +++ b/pages/wronginfo.html @@ -0,0 +1,19 @@ + + + + + Wrong information + + + +
+ +
+

Wrong Information

+

Please go back and try again. If you need to register; you may do so +

+
+ + diff --git a/server.js b/server.js index b5ac42e..7bb66ff 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const http = require("http"); const fs = require("fs"); const qs = require("querystring"); const dat = require("./data/data.json"); +var logged = false; const server = http.createServer((req, res) => { if (req.url === "/styles/styles.css") { @@ -93,6 +94,105 @@ const server = http.createServer((req, res) => { res.end(); }); } + + if (req.url === "/home" && logged === true) { + res.statusCode = 200; + res.setHeader("Content-Type", "text/html"); + fs.readFile("./pages/home.html", (error, data) => { + if (error) { + res.writeHead(404); + res.write("Error: File not found."); + } else { + res.write(data); + } + + res.end(); + }); + } + + if (req.url === "/not_logged_in") { + res.statusCode = 200; + res.setHeader("Content-Type", "text/html"); + fs.readFile("./pages/notlogged.html", (error, data) => { + if (error) { + res.writeHead(404); + res.write("Error: File not found."); + } else { + res.write(data); + } + + res.end(); + }); + } + + if (req.url === "/home" && !logged) { + res.statusCode = 200; + res.writeHead(301, { Location: "/not_logged_in" }); + res.end(); + } + + if (req.url === "/" && logged === true) { + res.statusCode = 200; + res.writeHead(301, { Location: "/home" }); + res.end(); + } + + if (req.url === "/unlog") { + res.statusCode = 200; + res.setHeader("Content-Type", "text/html"); + fs.readFile("./pages/logout.html", (error, data) => { + if (error) { + res.writeHead(404); + res.write("Error: File not found."); + } else { + logged = false; + res.write(data); + } + + res.end(); + }); + } + + if (req.url === "/wrong_info") { + res.statusCode = 200; + res.setHeader("Content-Type", "text/html"); + fs.readFile("./pages/wronginfo.html", (error, data) => { + if (error) { + res.writeHead(404); + res.write("Error: File not found."); + } else { + res.write(data); + } + + res.end(); + }); + } + + if (req.url === "/members" && req.method === "POST") { + res.statusCode = 200; + var body = ""; + + req.on("data", (data) => { + body = body + data; + }); + + req.on("end", () => { + var post = qs.parse(body); + dat.forEach(i => { + if (post.username === i.username && post.password === i.password) { + logged = true; + } + }); + + if (logged === true) { + res.writeHead(301, { Location: "/home" }); + } else { + res.writeHead(301, { Location: "/wrong_info" }); + } + + res.end(); + }) + } }); const port = process.env.port || 9000;