login logic and necessary pages.

This commit is contained in:
sweatshirt0 2023-02-20 20:10:41 -05:00
parent 5bf105c402
commit cc6b087b45
5 changed files with 165 additions and 0 deletions

16
pages/home.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charse="UTF-8" />
<title>Welcome Home</title>
<link rel="stylesheet" type="text/css" href="../styles/styles.css" />
</head>
<body>
<div class="whole-wrapper">
<div class="nav-wrapper">
<a class="logged-home-link" href="/home">Home</a>
<a class="logout-link" href="/unlog">Log Out</a>
</div>
</div>
</body>
</html>

15
pages/logout.html Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Logged out.</title>
<link rel="stylesheet" type="text/css" href="../styles/styles.css" />
</head>
<body>
<div class="whole-wrapper">
<div class="nav-wrapper">
<a class="home-link" href="/">Home</a>
</div>
</div>
</body>
</html>

15
pages/notlogged.html Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="URF-8" />
<title>Please Log In First.</title>
</link rel="stylesheet" type="text/css" href="./../styles/styles.css" />
</head>
<body>
<div class="whole-wrapper">
<div class="nav-wrapper">
<a class="home-link" href="/">Home</a>
</div>
</div>
</body>
</html>

19
pages/wronginfo.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Wrong information</title>
<link rel="stylesheet" type="text/css" href="./../styles/styles.css" />
</head>
<body>
<div class="whole-wrapper">
<div class="nav-wrapper">
<a class="home-link" href="/">Home</a>
</div>
<div class="wrong-info-content-wrapper">
<h1 class="wrong-info-title">Wrong Information</h1>
<p class="wrong-info-message">Please go back and try again. If you need to register; you may do so <a class="signup-link-land" href="/signup">here</a>
</div>
</div>
</body>
</html>

100
server.js
View File

@ -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;