2018-09-20 15:38:12 +00:00
|
|
|
import * as express from "express";
|
2018-09-28 21:33:28 +00:00
|
|
|
import * as cors from "cors";
|
2018-09-20 15:38:12 +00:00
|
|
|
|
|
|
|
import * as bodyparser from "body-parser";
|
|
|
|
|
|
|
|
import { isAuthenticated, performLogin } from "./security/auth";
|
|
|
|
|
2018-09-23 20:17:35 +00:00
|
|
|
import UserRouter from "./api/user";
|
|
|
|
import ClassRouter from "./api/class";
|
|
|
|
import LevelRouter from "./api/level";
|
|
|
|
|
2018-09-20 15:38:12 +00:00
|
|
|
const baseRouter = express.Router();
|
|
|
|
const authRouter = express.Router();
|
|
|
|
|
|
|
|
authRouter.use(bodyparser.json());
|
|
|
|
authRouter.use(async (req, res, next) => {
|
2018-09-21 14:27:25 +00:00
|
|
|
const token = req.get("Token");
|
|
|
|
if (token) {
|
2018-09-20 15:38:12 +00:00
|
|
|
// Check if were authenticated
|
|
|
|
const auth = await isAuthenticated(token);
|
|
|
|
if (auth)
|
|
|
|
next();
|
|
|
|
else
|
|
|
|
res.send({
|
|
|
|
error: "401",
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.send({
|
|
|
|
error: "401",
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(bodyparser.json());
|
2018-09-28 21:33:28 +00:00
|
|
|
// app.use((req, res, next) => {
|
|
|
|
// // TODO: Change this to our domain
|
|
|
|
// res.append("Access-Control-Allow-Origin", "*");
|
|
|
|
// res.append("Access-Control-Allow-Headers", "Content-Type,Token");
|
|
|
|
// next();
|
|
|
|
// });
|
|
|
|
app.options("*", cors());
|
2018-09-23 20:17:35 +00:00
|
|
|
|
|
|
|
app.use("/api/level", LevelRouter);
|
|
|
|
app.use("/api/class", ClassRouter);
|
|
|
|
app.use("/api/user", UserRouter);
|
2018-09-24 11:36:42 +00:00
|
|
|
app.get("/api/levels", async (req, res) => {
|
|
|
|
const levels = [{
|
|
|
|
name: "Der Bauer auf dem Feld",
|
|
|
|
desc: "So fängt alles an: Du bist ein einfacher Bauer und musst dich die Karriereleiter mit deinen freshen Latein-Skills hinaufarbeiten",
|
|
|
|
level: 1,
|
|
|
|
done: true,
|
|
|
|
}, {
|
|
|
|
name: "???",
|
|
|
|
desc: "Warum schreibe ich überhaupt was?dsd dddddddddddddddddddddd",
|
|
|
|
level: 2,
|
|
|
|
done: false,
|
|
|
|
}];
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
|
|
|
levels,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2018-09-23 20:17:35 +00:00
|
|
|
app.get("/api/health", (req, res) => {
|
2018-09-20 15:38:12 +00:00
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
|
|
|
msg: "lol",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2018-09-23 20:17:35 +00:00
|
|
|
app.post("/api/login", async (req, res) => {
|
2018-09-20 15:38:12 +00:00
|
|
|
const { body } = req;
|
|
|
|
|
|
|
|
console.log("Stub: /login");
|
|
|
|
|
|
|
|
// Check if all arguments were sent
|
2018-09-21 14:27:25 +00:00
|
|
|
if (!body || !("username" in body) || !("password" in body)) {
|
2018-09-20 15:38:12 +00:00
|
|
|
res.send({
|
|
|
|
error: "400",
|
|
|
|
data: {
|
|
|
|
msg: "Username or password not specified",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to log the user in
|
2018-09-21 14:27:25 +00:00
|
|
|
const userData = await performLogin(body.username, body.password)
|
2018-09-20 15:38:12 +00:00
|
|
|
.catch((err) => {
|
2018-09-21 14:27:25 +00:00
|
|
|
console.log("Could not resolve login promise!", err);
|
|
|
|
|
2018-09-20 15:38:12 +00:00
|
|
|
// If anything was wrong, just tell the client
|
|
|
|
res.send({
|
|
|
|
error: "1",
|
|
|
|
data: {
|
|
|
|
msg: "Username or password is wrong",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: userData,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
app.listen(8080, () => {
|
|
|
|
console.log("Starting on port 8080");
|
|
|
|
});
|