-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (113 loc) · 3.9 KB
/
index.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
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
const express = require('express');
const { QuickDB } = require('quick.db');
const db = new QuickDB()
const app = express();
let config = require('./config.json')
app.use(express.json()); // for parsing application/json
const passport = require('passport');
const DiscordStrategy = require('passport-discord').Strategy;
const session = require('express-session');
app.use(session({
secret: config.secret,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
// Assuming you have routes for creating and deleting todos
app.post('/api/todo', (req, res) => {
// Assuming you're using req.body.todo to get the new todo
const newTodo = req.body;
// Add the new todo to the database...
let id = Math.random().toString(36).substring(7);
db.set("todos." +id, {
id: id,
title: newTodo.title,
description: newTodo.description,
dueDate: newTodo.dueDate,
priority: newTodo.priority,
});
console.log(`[${new Date().toISOString()}] [TODO] User ${req.user.id || "noone"} created a todo: ${newTodo}`);
res.status(200).send('Todo created');
});
app.delete('/api/todos/:id', async (req, res) => {
// Assuming you're using req.params.id to get the id of the todo to delete
const todoId = req.params.id;
// Delete the todo from the database...
db.delete("todos." + todoId);
console.log(`[${new Date().toISOString()}] [TODO] User ${req.user.id || "noone"} deleted a todo: ${todoId}`);
res.status(200).send('Todo deleted');
});
app.get('/api/todos', async (req, res) => {
// Fetch all todos from the database...
const todos = await db.get("todos");
console.log(todos)
if(!todos) return res.status(404).send('No todos found')
if(todos === undefined) return res.status(404).send('No todos found')
if(todos === null) return res.status(404).send('No todos found')
console.log(`[${new Date().toISOString()}] [TODO] User ${req?.user?.id || "noone"} fetched all todos`);
res.status(200).json(todos);
});
passport.use(new DiscordStrategy({
clientID: config.clientid,
clientSecret: config.clientsecret,
callbackURL: config.callbackurl,
scope: ['identify', 'email']
},
function(accessToken, refreshToken, profile, cb) {
if (profile.id === config.alloweduser) {
return cb(null, profile);
} else {
return cb(null, false, { message: 'Not allowed' });
}
}
));
app.get('/auth/discord',
passport.authenticate('discord'));
app.get('/auth/discord/callback',
passport.authenticate('discord', { failureRedirect: '/readonly' }),
function(req, res) {
console.log(`[${new Date().toISOString()}] [AUTH] User ${req.user.id} logged in`);
// Successful authentication, redirect home.
res.redirect('/');
});
app.get('/', (req, res) => {
if (!req.user) {
res.redirect('/notloggedin');
} else if (req.user.id !== config.alloweduser) {
res.redirect('/readonly');
} else {
res.sendFile (__dirname + '/index.html');
}
});
app.get('/readonly', checkAuthentication, (req, res) => {
res.sendFile (__dirname + '/readonly.html');
});
app.get('/notloggedin', (req, res) => {
res.sendFile (__dirname + '/notloggedin.html');
});
app.get('/logout', (req, res) => {
req.session.destroy(function(err) {
if (err) {
console.log(`[${new Date().toISOString()}] [ERROR] ${err}`);
} else {
res.redirect('/notloggedin');
}
});
});
// Middleware to check if the user is authenticated
function checkAuthentication(req, res, next) {
if (req.user) {
next();
} else {
res.redirect('/notloggedin');
}
}
// Rest of your routes...
app.listen(config.port, () => console.log(`[${new Date().toISOString()}] [WEB] Accessible on http://localhost:${config.port}`));