2018-09-20 15:38:12 +00:00
|
|
|
import * as express from "express";
|
|
|
|
|
|
|
|
import * as bodyparser from "body-parser";
|
|
|
|
|
|
|
|
import { isAuthenticated, performLogin } from "./security/auth";
|
|
|
|
|
|
|
|
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: {},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
authRouter.get("/class/:id/topTen", async (req, res) => {
|
|
|
|
if (!req.params) {
|
|
|
|
res.send({
|
|
|
|
error: "400",
|
|
|
|
data: {
|
|
|
|
msg: "No class specified",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Stub: /auth/class/:id/topTen");
|
2018-09-23 19:52:29 +00:00
|
|
|
|
|
|
|
let users: any[] = [];
|
|
|
|
let nr = 10;
|
|
|
|
for (let i = 0; i < 10; i++)
|
|
|
|
users = users.concat({
|
|
|
|
username: `Test User ${i}`,
|
|
|
|
score: 100 * i,
|
|
|
|
level: Math.floor(Math.random() * Math.floor(10)),
|
|
|
|
nr: nr--,
|
|
|
|
});
|
|
|
|
|
2018-09-20 15:38:12 +00:00
|
|
|
// TODO: Implement
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
2018-09-23 19:52:29 +00:00
|
|
|
topTen: users,
|
2018-09-20 15:38:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
authRouter.get("/level/:id/vocab", async (req, res) => {
|
2018-09-21 14:27:25 +00:00
|
|
|
// TODO: Implement
|
|
|
|
console.log("Stub: /auth/level/:id/vocab");
|
|
|
|
|
2018-09-20 15:38:12 +00:00
|
|
|
if (!req.params) {
|
|
|
|
res.send({
|
|
|
|
error: "400",
|
|
|
|
data: {
|
|
|
|
msg: "No level specified",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
|
|
|
vocab: [{
|
|
|
|
german: ["Wein"],
|
|
|
|
hint: "Worte auf '-um' sind meistens NeutrUM",
|
|
|
|
type: 0,
|
|
|
|
latin: {
|
|
|
|
grundform: "Vinum",
|
|
|
|
genitiv: "Vini",
|
|
|
|
genus: "Neutrum"
|
|
|
|
},
|
|
|
|
id: 0
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-09-23 19:52:29 +00:00
|
|
|
authRouter.get("/user/nextLevel", async (req, res) => {
|
|
|
|
console.log("Stub: /auth/user/nextLevel");
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
|
|
|
name: "Test level",
|
|
|
|
desc: "Just a test",
|
|
|
|
level: 3,
|
|
|
|
|
|
|
|
done: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2018-09-20 15:38:12 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(bodyparser.json());
|
|
|
|
|
|
|
|
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");
|
2018-09-21 14:27:25 +00:00
|
|
|
next();
|
2018-09-20 15:38:12 +00:00
|
|
|
});
|
|
|
|
app.use("/auth", authRouter);
|
|
|
|
app.get("/health", (req, res) => {
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
|
|
|
msg: "lol",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
app.post("/login", async (req, res) => {
|
|
|
|
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");
|
|
|
|
});
|