refactor: MONOREPO
This commit is contained in:
parent
4c9e328ad0
commit
909149fdc7
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
node_modules/
|
||||
.cache/
|
||||
dist/
|
||||
**/node_modules/
|
||||
**/.cache/
|
||||
**/dist/
|
||||
|
29
backend/package.json
Normal file
29
backend/package.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "lateinicusserver",
|
||||
"version": "0.1.0",
|
||||
"description": "The backend server for Lateinicus",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node dist/main.js",
|
||||
"build": "tsc -p tsconfig.json"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://gitlab.com/Polynomdivision/lateinicusserver.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/Polynomdivision/lateinicusserver/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/Polynomdivision/lateinicusserver#readme",
|
||||
"dependencies": {
|
||||
"express": "4.16.3",
|
||||
"body-parser": "1.18.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "4.16.0",
|
||||
"typescript": "3.0.3"
|
||||
}
|
||||
}
|
150
backend/src/main.ts
Normal file
150
backend/src/main.ts
Normal file
@ -0,0 +1,150 @@
|
||||
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) => {
|
||||
if ("token" in req.body || req.get("token")) {
|
||||
const token = req.body.token || req.get("token");
|
||||
|
||||
// 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");
|
||||
// TODO: Implement
|
||||
res.send({
|
||||
error: "0",
|
||||
data: {
|
||||
topTen: [{
|
||||
username: "User1",
|
||||
level: 5,
|
||||
score: 200,
|
||||
}, {
|
||||
username: "User2",
|
||||
level: 4,
|
||||
score: 100,
|
||||
}],
|
||||
}
|
||||
});
|
||||
});
|
||||
authRouter.get("/level/:id/vocab", async (req, res) => {
|
||||
if (!req.params) {
|
||||
res.send({
|
||||
error: "400",
|
||||
data: {
|
||||
msg: "No level specified",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("Stub: /auth/level/:id/vocab");
|
||||
// TODO: Implement
|
||||
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
|
||||
}],
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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");
|
||||
|
||||
if (res.method === "OPTIONS") {
|
||||
// TODO: Send 200
|
||||
res.end();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
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
|
||||
if (!body || !body.hasOwnProperty("username") || !body.hasOwnProperty("hash")) {
|
||||
res.send({
|
||||
error: "400",
|
||||
data: {
|
||||
msg: "Username or password not specified",
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to log the user in
|
||||
const userData = await performLogin(body.username, body.hash)
|
||||
.catch((err) => {
|
||||
// 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");
|
||||
});
|
27
backend/src/security/auth.ts
Normal file
27
backend/src/security/auth.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { pbkdf2Sync } from "crypto";
|
||||
|
||||
export function isAuthenticated(token: string): Promise<boolean> {
|
||||
return new Promise((res, rej) => {
|
||||
// TODO
|
||||
res(true);
|
||||
});
|
||||
}
|
||||
|
||||
export function performLogin(username: string, password: string): Promise<any | {}> {
|
||||
return new Promise((res, rej) => {
|
||||
// Hash the password
|
||||
// TODO: Fetch the salt
|
||||
const salt = "";
|
||||
const hash = pbkdf2Sync(password, salt, 50000, 512, "sha512").toString("hex");
|
||||
|
||||
// TODO: Look up the user, compare hashes and send the returned user
|
||||
res({
|
||||
username: "Polynom",
|
||||
uid: "1",
|
||||
showWelcome: false,
|
||||
classId: "test",
|
||||
|
||||
sessionToken: "abc123",
|
||||
});
|
||||
});
|
||||
};
|
13
backend/tsconfig.json
Normal file
13
backend/tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESnext",
|
||||
"module": "CommonJS",
|
||||
"noImplicitAny": true,
|
||||
"removeComments": true,
|
||||
"sourceMap": true,
|
||||
|
||||
"outDir": "./dist/",
|
||||
|
||||
"baseUrl": "./src/"
|
||||
}
|
||||
}
|
Before Width: | Height: | Size: 2.5 MiB After Width: | Height: | Size: 2.5 MiB |
Reference in New Issue
Block a user