-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
234 lines (217 loc) · 4.7 KB
/
index.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import express, { Express } from 'express';
const app: Express = express()
const fs = require("fs")
import sessions from 'express-session';
const oneDay = 86400000
import { PrismaClient } from "@prisma/client";
const prisma: PrismaClient = new PrismaClient();
const crypto = require("crypto")
declare module 'express-session' {
interface SessionData {
userID: string
}
}
app.use(sessions({
secret: ".f2.97rrh34?r318b24!82rb",
saveUninitialized: true,
cookie: { maxAge: oneDay },
resave: false
}));
app.listen(80, () => {
console.log("Server started on port 80")
})
app.get("/script.js", (req, res) => {
res.write(fs.readFileSync("./public/script.js"));
res.end();
})
app.get("/style.css", (req, res) => {
res.write(fs.readFileSync("./public/style.css"));
res.end();
})
app.get("/", (req, res) => {
if (req.session.userID) {
res.write(fs.readFileSync("./public/index.html"));
res.end();
}
else {
res.write(fs.readFileSync("./public/login.html"));
res.end();
}
})
app.get("/icons/add.png", (req, res) => {
res.sendFile(__dirname + "/public/icons/add.png")
})
app.get("/login", async (req, res) => {
if (req.query.username && req.query.password) {
let username = req.query.username as string
let password = crypto.createHash('sha256').update(req.query.password).digest('base64') as string
let user = await prisma.user.findFirst({
where: {
name: username,
password: password
}
})
if (user) {
req.session.userID = user.id
res.json({"status": 1})
res.end()
}
else {
res.json({"status": 0})
res.end()
}
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/getUserData", async (req, res) => {
if (req.session.userID) {
let userData = await prisma.user.findFirst({
where: {
id: req.session.userID
},
select: {
name: true,
id: true,
todolists: {
select: {
id: true,
name: true,
todos: {
select: {
id: true,
name: true,
done: true
}
}
}
}
}
})
res.json(userData)
res.end()
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/addList", async (req, res) => {
if (req.session.userID && req.query.listName) {
let listName = req.query.listName as string
let list = await prisma.toDoList.create({
data: {
name: listName,
user: {
connect: {
id: req.session.userID
}
}
},
select: {
id: true,
name: true
}
})
res.json(list)
res.end()
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/addToDo", async (req, res) => {
if (req.session.userID && req.query.todo && req.query.todolist) {
let todoName = req.query.todo as string
let todolist = req.query.todolist as string
let todo = await prisma.toDoItem.create({
data: {
name: todoName,
done: false,
todolist: {
connect: {
id: todolist
}
}
},
select: {
id: true,
name: true
}
})
res.json(todo)
res.end()
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/updateToDo", async (req, res) => {
if (req.query.todoID && req.query.todoDone != undefined) {
let todoID = req.query.todoID as string
let todoDone = req.query.todoDone as string
let done = todoDone=="1"?true:false
await prisma.toDoItem.update({
where: {
id: todoID
},
data: {
done: done
}
})
res.json({"status": 1})
res.end()
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/addUser", async (req, res) => {
if (req.query.username && req.query.password) {
let username = req.query.username as string
let password = crypto.createHash('sha256').update(req.query.password).digest('base64') as string
await prisma.user.create({
data: {
name: username,
password: password
}
})
res.json({"status": 1})
res.end()
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/canRegister", async (req, res) => {
if (req.query.username) {
let username = req.query.username as string
let user = await prisma.user.findUnique({
where: {
name: username
}
})
if (user) {
res.json({"status": 0})
res.end()
}
else {
res.json({"status": 1})
res.end()
}
}
else {
res.json({"status": 0})
res.end()
}
})
app.get("/logout", (req, res) => {
req.session.userID = undefined
res.redirect("/")
})