log + auth

This commit is contained in:
Killer Boss Original 2022-07-05 15:10:58 +02:00 committed by GitHub
parent da2da30493
commit 0e136b62c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,9 +29,11 @@ app.listen(client.config.port,
app.use(express.static('public')); app.use(express.static('public'));
app.use(cors()); app.use(cors());
async function authenticate(token) { async function authenticate(token, what) {
if (!client.config.auth.includes(token)) { if (!client.config.auth.includes(token)) {
return; response.send('ERROR - Not auth')
} else {
console.log(token + ' do '+ what)
} }
} }
@ -41,7 +43,7 @@ app.get('/api/:auth/database', alldata);
function alldata(request, response) { function alldata(request, response) {
var token = request.params.auth; var token = request.params.auth;
authenticate(token) authenticate(token, 'alldata')
response.send(elements); response.send(elements);
} }
@ -51,16 +53,16 @@ app.get('/api/:auth/database/:element/', searchElement);
async function searchElement(request, response) { async function searchElement(request, response) {
var token = request.params.auth; var token = request.params.auth;
authenticate(token)
var word = request.params.element; var word = request.params.element;
search = 'search '+ word
authenticate(token, search)
var elements = await db.get(word) var elements = await db.get(word)
if(elements) { if(elements) {
var reply = elements; var reply = elements;
} } else {
else {
var reply = { var reply = {
status:"Not Found" status:"Not Found"
} }
@ -75,20 +77,21 @@ app.get('/api/:auth/database/:element/set/:data', set);
async function set(request, response) { async function set(request, response) {
var token = request.params.auth; var token = request.params.auth;
authenticate(token)
var element = request.params.element; var element = request.params.element;
var data = request.params.data; var data = request.params.data;
set = 'set '+element+' to '+data
authenticate(token, set)
await db.set(element, data) await db.set(element, data)
const res = await db.get(element) const res = await db.get(element)
if(res) { if(res) {
var reply = res; var reply = res;
} } else {
else {
var reply = { var reply = {
status:"Not Found" status:"Not Found"
} }
} }
response.send(reply); response.send(reply);
} }