2021-10-17 18:57:26 +02:00
|
|
|
import 'dotenv/config'
|
2021-10-17 18:48:39 +02:00
|
|
|
|
2021-10-17 20:55:00 +02:00
|
|
|
import express from 'express'
|
|
|
|
|
import jwt from 'jsonwebtoken'
|
2021-10-17 21:45:05 +02:00
|
|
|
import morgan from 'morgan'
|
2021-10-17 22:17:18 +02:00
|
|
|
import cors from 'cors'
|
2021-10-17 21:45:05 +02:00
|
|
|
|
2021-10-17 20:55:00 +02:00
|
|
|
import auth from './Auth.js'
|
|
|
|
|
import db from './Database.js'
|
2021-10-17 23:47:52 +02:00
|
|
|
import jg from './JusticeGuard.js'
|
2021-10-17 20:55:00 +02:00
|
|
|
|
|
|
|
|
const protect = (req, res, next) => {
|
|
|
|
|
const authHeader = req.headers.authorization;
|
|
|
|
|
|
|
|
|
|
if (!authHeader) {
|
|
|
|
|
res.sendStatus(401)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const token = authHeader.split(' ')[1];
|
|
|
|
|
|
|
|
|
|
jwt.verify(token, process.env.secret, (err, user) => {
|
|
|
|
|
if (err)
|
|
|
|
|
return res.sendStatus(403)
|
|
|
|
|
|
|
|
|
|
req.user = user
|
|
|
|
|
next()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 22:53:33 +02:00
|
|
|
const neededArguments = (args) => {
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
|
const arg = args[i];
|
|
|
|
|
if (!req.body[arg])
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error: "missing argument " + arg
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 18:57:26 +02:00
|
|
|
const app = express()
|
2021-10-17 21:45:05 +02:00
|
|
|
app.use(morgan('combined'))
|
2021-10-17 20:55:00 +02:00
|
|
|
app.use(express.json())
|
2021-10-17 22:17:18 +02:00
|
|
|
app.use(cors())
|
2021-10-17 20:55:00 +02:00
|
|
|
|
|
|
|
|
app.get("/", (_, res) => {
|
|
|
|
|
res.json({ message: "Hello! API here" })
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get("/secured", protect, (req, res) => {
|
|
|
|
|
res.json({ message: "You can see it!", payload: req.user })
|
|
|
|
|
})
|
|
|
|
|
|
2021-10-17 22:53:33 +02:00
|
|
|
app.post("/signup", neededArguments(['name']), async (req, res) => {
|
2021-10-17 21:12:42 +02:00
|
|
|
const user = await auth.createAccount(req.body.name)
|
2021-10-17 20:55:00 +02:00
|
|
|
|
2021-10-17 21:12:42 +02:00
|
|
|
if (user.error)
|
2021-10-17 20:55:00 +02:00
|
|
|
return res.status(400).json({
|
2021-10-17 21:12:42 +02:00
|
|
|
error: user.error
|
2021-10-17 20:55:00 +02:00
|
|
|
})
|
|
|
|
|
|
2021-10-17 21:12:42 +02:00
|
|
|
res.json(user)
|
|
|
|
|
})
|
|
|
|
|
|
2021-10-17 22:53:33 +02:00
|
|
|
app.post("/login", neededArguments(['key']), async (req, res) => {
|
|
|
|
|
const user = auth.login(req.body.key)
|
2021-10-17 21:12:42 +02:00
|
|
|
|
|
|
|
|
if (user.error)
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error: user.error
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
res.json(user)
|
2021-10-17 20:55:00 +02:00
|
|
|
})
|
2021-10-17 18:48:39 +02:00
|
|
|
|
2021-10-17 23:47:52 +02:00
|
|
|
app.post("/record", protect, neededArguments(['points', 'shoots', 'time']), jg, async (req, res) => {
|
2021-10-17 23:00:29 +02:00
|
|
|
db.updateRecord(req.user.name, req.body.points)
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
status: "ok"
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-10-17 23:55:58 +02:00
|
|
|
app.get("/top", (req, res) => {
|
|
|
|
|
res.json({
|
|
|
|
|
status: "ok",
|
|
|
|
|
records: db.getTop()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-10-17 20:55:00 +02:00
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
|
if (err.name === 'UnauthorizedError') {
|
|
|
|
|
res.status(401).send('invalid token...');
|
|
|
|
|
}
|
2021-10-17 18:48:39 +02:00
|
|
|
});
|
|
|
|
|
|
2021-10-17 20:55:00 +02:00
|
|
|
await db.read()
|
2021-10-17 18:57:26 +02:00
|
|
|
app.listen(3000)
|