forked from FlagClicked/FlagClicked
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (136 loc) · 4.27 KB
/
Copy pathindex.js
File metadata and controls
150 lines (136 loc) · 4.27 KB
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
const REPLIT_URL = "www.whenflagclicked.org"; // Please change this URL to your replit URL before starting!
const express = require("express");
const fs = require("fs");
const readDir = require("./libs/readDir");
const Database = require("@replit/database");
const db = new Database();
const addLibs = require("./libs/add-libs");
const auth = require("./libs/auth");
const fetch = require("node-fetch");
// db.list().then((keys) => {for (key of keys) {db.delete(key)}})
const app = express();
app.use(express.text());
const pages = {};
readDir("/pages/", {
callback({ dir, file, isFile, pageDir }) {
if (file == "index.html") {
pages[pageDir] = dir + file;
}
},
});
pageDir = "/assets/";
readDir("/assets/", {
callback({ dir, file, isFile }) {
if (!isFile) {
pageDir += file + "/";
} else {
pages[dir + file] = dir + file;
}
},
});
console.log(pages);
for (const page in pages) {
app.get(page, (req, res) => {
console.log(pages[page].split(".")[1]);
if (pages[page].split(".")[1] === "html") {
const html = fs.readFileSync("." + pages[page], "utf8");
res.setHeader("Content-type", "text/html");
res.send(addLibs(html));
} else {
//res.contentType(__dirname + pages[page]);
//res.setHeader('Content-type','image/jpg');
res.sendFile(__dirname + pages[page]);
}
});
}
app.get("/api/tutorials/:id", (req, res) => {
db.get(`tutorial-${req.params.id}`).then((value) => {
res.json(value || { error: "no tutorial found" });
});
});
app.get("/tutorials/:id", (req, res) => {
const html = fs.readFileSync("./pages/tutorials/index.html", "utf8");
res.setHeader("Content-type", "text/html");
res.send(addLibs(html));
});
app.post("/api/new", (req, res) => {
res.header("Access-Control-Allow-Origin", REPLIT_URL);
res.set("Access-Control-Allow-Methods", "POST");
if ("POST" !== req.method)
return res.status(403).json({ error: "invalid method" });
var cookie = auth.getCookie("token", req.headers.cookie);
if (!cookie) return res.status(403).json({ error: "no token" });
auth
.getSession(cookie)
.catch((err) => {
res.status(403).json({ error: "invalid token" });
return false;
})
.then((user) => {
if (!res) return;
db.list("tutorial-").then((matches) => {
var data = {
id: matches.length + 1,
body: req.body,
created: new Date().toUTCString(),
author: user.name,
};
db.set(`tutorial-${matches.length + 1}`, data).then(() => {
res.json({ tutorialId: matches.length + 1 });
});
});
});
});
app.get("/login", (req, res) => {
// endpoint that redirects the user to FluffyScratch
res.redirect(
`https://fluffyscratch.hampton.pw/auth/getKeys/v2?redirect=${Buffer.from(
REPLIT_URL + "/login/finish",
"utf-8"
).toString("base64")}`
);
});
// /login/finish - Finishes the logging-in process from FluffyScratch. It issues a cookie to the user
app.get("/login/finish", async (req, res) => {
let error = false;
if (req.query.privateCode) {
var username = await auth
.checkIfFluffyResponseValid(req.query.privateCode)
.catch((err) => {
error = true;
});
if (username) {
var session = await auth.createSession(username);
res.cookie("token", session.token, { path: "/" });
}
} else error = true;
res.redirect("/?error=" + error);
});
app.get("/login/me", (req, res) => {
// Returns <user> object.
if (!req.query.token) return res.json({ error: "no token provided" });
auth
.getSession(req.query.token)
.catch((err) => {
return res.json({ error: "invalid token" });
})
.then((resp) => {
if (!resp) return;
resp.sessions = null; // remove this so we mask sessions away from the client.
res.json(resp);
});
});
app.get("/login/delete", (req, res) => {
var cookie = auth.getCookie("token", req.headers.cookie);
if (!cookie) return res.status(403).json({ error: "invalid token" });
auth
.deleteSession(cookie)
.catch((err) => {
return res.json({ error: "invalid token" });
})
.then(() => {
res.clearCookie("token", { path: "/" }); // clear the cookie from the client
res.redirect("/");
});
});
app.listen(3000, () => console.log("server started"));