Update record

This commit is contained in:
KGrzeg 2021-10-17 23:00:29 +02:00
parent 6843b76ebf
commit a38072214e
3 changed files with 27 additions and 2 deletions

View file

@ -14,8 +14,7 @@ class Database {
async read() { async read() {
await this.db.read(); await this.db.read();
this.db.data = this.db.data || { this.db.data = this.db.data || {
users: [], users: []
records: []
} }
} }
@ -32,6 +31,15 @@ class Database {
getUserByPassword(password) { getUserByPassword(password) {
return this.db.data.users.find(user => user.password === password) return this.db.data.users.find(user => user.password === password)
} }
async updateRecord(username, record) {
const user = this.db.data.users.find(user => user.name == username)
if (record > user.record) {
user.record = record
await this.db.write()
}
}
} }
export default new Database export default new Database

8
server/JusticeGuard.js Normal file
View file

@ -0,0 +1,8 @@
export default function (req, res, next) {
//available keys 'points', 'shoots', 'time'
//TODO: do some checks
next()
}

View file

@ -7,6 +7,7 @@ import cors from 'cors'
import auth from './Auth.js' import auth from './Auth.js'
import db from './Database.js' import db from './Database.js'
import jg from './JusticeGuard'
const protect = (req, res, next) => { const protect = (req, res, next) => {
const authHeader = req.headers.authorization; const authHeader = req.headers.authorization;
@ -75,6 +76,14 @@ app.post("/login", neededArguments(['key']), async (req, res) => {
res.json(user) res.json(user)
}) })
app.post("/record", neededArguments(['points', 'shoots', 'time']), jg, async (req, res) => {
db.updateRecord(req.user.name, req.body.points)
res.json({
status: "ok"
})
})
app.use(function (err, req, res, next) { app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') { if (err.name === 'UnauthorizedError') {
res.status(401).send('invalid token...'); res.status(401).send('invalid token...');