From 909149fdc72a6361b74113ef44c40d6e14444d2a Mon Sep 17 00:00:00 2001 From: Alexander Polynomdivision Date: Thu, 20 Sep 2018 17:38:12 +0200 Subject: [PATCH] refactor: MONOREPO --- .gitignore | 6 +- backend/package.json | 29 ++++ backend/src/main.ts | 150 ++++++++++++++++++ backend/src/security/auth.ts | 27 ++++ backend/tsconfig.json | 13 ++ build.sh => frontend/build.sh | 0 .../package-lock.json | 0 package.json => frontend/package.json | 0 {public => frontend/public}/assets/stock.jpg | Bin {public => frontend/public}/index.css | 0 {public => frontend/public}/index.html | 0 {src => frontend/src}/actions/index.ts | 0 .../levenshtein/__tests__/levenshtein.test.ts | 0 .../src}/algorithms/levenshtein/index.ts | 0 {src => frontend/src}/algorithms/sm2/index.ts | 0 {src => frontend/src}/components/Drawer.tsx | 0 .../src}/components/SummaryTable.tsx | 0 {src => frontend/src}/components/app.tsx | 0 {src => frontend/src}/components/loading.tsx | 0 .../src}/components/scoreboard.tsx | 0 {src => frontend/src}/config.ts | 0 .../src}/containers/Application.ts | 0 {src => frontend/src}/containers/Drawer.ts | 0 {src => frontend/src}/containers/LevelList.ts | 0 {src => frontend/src}/containers/LevelPage.ts | 0 {src => frontend/src}/containers/LoginPage.ts | 0 {src => frontend/src}/containers/Review.ts | 0 .../src}/containers/SummaryPage.ts | 0 {src => frontend/src}/index.css | 0 {src => frontend/src}/index.html | 0 {src => frontend/src}/index.tsx | 0 {src => frontend/src}/models/learner.ts | 0 {src => frontend/src}/models/level.ts | 0 {src => frontend/src}/models/review.ts | 0 {src => frontend/src}/models/user.ts | 0 {src => frontend/src}/models/vocab.ts | 0 {src => frontend/src}/pages/dashboard.tsx | 0 {src => frontend/src}/pages/intro.tsx | 0 {src => frontend/src}/pages/level.tsx | 0 {src => frontend/src}/pages/levelList.tsx | 0 {src => frontend/src}/pages/login.tsx | 0 {src => frontend/src}/pages/review.tsx | 0 {src => frontend/src}/pages/summary.tsx | 0 {src => frontend/src}/reducers/index.ts | 0 {src => frontend/src}/security/AuthRoute.tsx | 0 {src => frontend/src}/security/Token.ts | 0 .../src}/utils/__test__/queue.test.ts | 0 {src => frontend/src}/utils/date.ts | 0 {src => frontend/src}/utils/queue.ts | 0 tsconfig.json => frontend/tsconfig.json | 0 50 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 backend/package.json create mode 100644 backend/src/main.ts create mode 100644 backend/src/security/auth.ts create mode 100644 backend/tsconfig.json rename build.sh => frontend/build.sh (100%) rename package-lock.json => frontend/package-lock.json (100%) rename package.json => frontend/package.json (100%) rename {public => frontend/public}/assets/stock.jpg (100%) rename {public => frontend/public}/index.css (100%) rename {public => frontend/public}/index.html (100%) rename {src => frontend/src}/actions/index.ts (100%) rename {src => frontend/src}/algorithms/levenshtein/__tests__/levenshtein.test.ts (100%) rename {src => frontend/src}/algorithms/levenshtein/index.ts (100%) rename {src => frontend/src}/algorithms/sm2/index.ts (100%) rename {src => frontend/src}/components/Drawer.tsx (100%) rename {src => frontend/src}/components/SummaryTable.tsx (100%) rename {src => frontend/src}/components/app.tsx (100%) rename {src => frontend/src}/components/loading.tsx (100%) rename {src => frontend/src}/components/scoreboard.tsx (100%) rename {src => frontend/src}/config.ts (100%) rename {src => frontend/src}/containers/Application.ts (100%) rename {src => frontend/src}/containers/Drawer.ts (100%) rename {src => frontend/src}/containers/LevelList.ts (100%) rename {src => frontend/src}/containers/LevelPage.ts (100%) rename {src => frontend/src}/containers/LoginPage.ts (100%) rename {src => frontend/src}/containers/Review.ts (100%) rename {src => frontend/src}/containers/SummaryPage.ts (100%) rename {src => frontend/src}/index.css (100%) rename {src => frontend/src}/index.html (100%) rename {src => frontend/src}/index.tsx (100%) rename {src => frontend/src}/models/learner.ts (100%) rename {src => frontend/src}/models/level.ts (100%) rename {src => frontend/src}/models/review.ts (100%) rename {src => frontend/src}/models/user.ts (100%) rename {src => frontend/src}/models/vocab.ts (100%) rename {src => frontend/src}/pages/dashboard.tsx (100%) rename {src => frontend/src}/pages/intro.tsx (100%) rename {src => frontend/src}/pages/level.tsx (100%) rename {src => frontend/src}/pages/levelList.tsx (100%) rename {src => frontend/src}/pages/login.tsx (100%) rename {src => frontend/src}/pages/review.tsx (100%) rename {src => frontend/src}/pages/summary.tsx (100%) rename {src => frontend/src}/reducers/index.ts (100%) rename {src => frontend/src}/security/AuthRoute.tsx (100%) rename {src => frontend/src}/security/Token.ts (100%) rename {src => frontend/src}/utils/__test__/queue.test.ts (100%) rename {src => frontend/src}/utils/date.ts (100%) rename {src => frontend/src}/utils/queue.ts (100%) rename tsconfig.json => frontend/tsconfig.json (100%) diff --git a/.gitignore b/.gitignore index d9e2efb..1e9062d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -node_modules/ -.cache/ -dist/ +**/node_modules/ +**/.cache/ +**/dist/ diff --git a/backend/package.json b/backend/package.json new file mode 100644 index 0000000..29242a3 --- /dev/null +++ b/backend/package.json @@ -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" + } +} diff --git a/backend/src/main.ts b/backend/src/main.ts new file mode 100644 index 0000000..8495932 --- /dev/null +++ b/backend/src/main.ts @@ -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"); +}); diff --git a/backend/src/security/auth.ts b/backend/src/security/auth.ts new file mode 100644 index 0000000..8d4a57f --- /dev/null +++ b/backend/src/security/auth.ts @@ -0,0 +1,27 @@ +import { pbkdf2Sync } from "crypto"; + +export function isAuthenticated(token: string): Promise { + return new Promise((res, rej) => { + // TODO + res(true); + }); +} + +export function performLogin(username: string, password: string): Promise { + 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", + }); + }); +}; diff --git a/backend/tsconfig.json b/backend/tsconfig.json new file mode 100644 index 0000000..ea7264c --- /dev/null +++ b/backend/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ESnext", + "module": "CommonJS", + "noImplicitAny": true, + "removeComments": true, + "sourceMap": true, + + "outDir": "./dist/", + + "baseUrl": "./src/" + } +} diff --git a/build.sh b/frontend/build.sh similarity index 100% rename from build.sh rename to frontend/build.sh diff --git a/package-lock.json b/frontend/package-lock.json similarity index 100% rename from package-lock.json rename to frontend/package-lock.json diff --git a/package.json b/frontend/package.json similarity index 100% rename from package.json rename to frontend/package.json diff --git a/public/assets/stock.jpg b/frontend/public/assets/stock.jpg similarity index 100% rename from public/assets/stock.jpg rename to frontend/public/assets/stock.jpg diff --git a/public/index.css b/frontend/public/index.css similarity index 100% rename from public/index.css rename to frontend/public/index.css diff --git a/public/index.html b/frontend/public/index.html similarity index 100% rename from public/index.html rename to frontend/public/index.html diff --git a/src/actions/index.ts b/frontend/src/actions/index.ts similarity index 100% rename from src/actions/index.ts rename to frontend/src/actions/index.ts diff --git a/src/algorithms/levenshtein/__tests__/levenshtein.test.ts b/frontend/src/algorithms/levenshtein/__tests__/levenshtein.test.ts similarity index 100% rename from src/algorithms/levenshtein/__tests__/levenshtein.test.ts rename to frontend/src/algorithms/levenshtein/__tests__/levenshtein.test.ts diff --git a/src/algorithms/levenshtein/index.ts b/frontend/src/algorithms/levenshtein/index.ts similarity index 100% rename from src/algorithms/levenshtein/index.ts rename to frontend/src/algorithms/levenshtein/index.ts diff --git a/src/algorithms/sm2/index.ts b/frontend/src/algorithms/sm2/index.ts similarity index 100% rename from src/algorithms/sm2/index.ts rename to frontend/src/algorithms/sm2/index.ts diff --git a/src/components/Drawer.tsx b/frontend/src/components/Drawer.tsx similarity index 100% rename from src/components/Drawer.tsx rename to frontend/src/components/Drawer.tsx diff --git a/src/components/SummaryTable.tsx b/frontend/src/components/SummaryTable.tsx similarity index 100% rename from src/components/SummaryTable.tsx rename to frontend/src/components/SummaryTable.tsx diff --git a/src/components/app.tsx b/frontend/src/components/app.tsx similarity index 100% rename from src/components/app.tsx rename to frontend/src/components/app.tsx diff --git a/src/components/loading.tsx b/frontend/src/components/loading.tsx similarity index 100% rename from src/components/loading.tsx rename to frontend/src/components/loading.tsx diff --git a/src/components/scoreboard.tsx b/frontend/src/components/scoreboard.tsx similarity index 100% rename from src/components/scoreboard.tsx rename to frontend/src/components/scoreboard.tsx diff --git a/src/config.ts b/frontend/src/config.ts similarity index 100% rename from src/config.ts rename to frontend/src/config.ts diff --git a/src/containers/Application.ts b/frontend/src/containers/Application.ts similarity index 100% rename from src/containers/Application.ts rename to frontend/src/containers/Application.ts diff --git a/src/containers/Drawer.ts b/frontend/src/containers/Drawer.ts similarity index 100% rename from src/containers/Drawer.ts rename to frontend/src/containers/Drawer.ts diff --git a/src/containers/LevelList.ts b/frontend/src/containers/LevelList.ts similarity index 100% rename from src/containers/LevelList.ts rename to frontend/src/containers/LevelList.ts diff --git a/src/containers/LevelPage.ts b/frontend/src/containers/LevelPage.ts similarity index 100% rename from src/containers/LevelPage.ts rename to frontend/src/containers/LevelPage.ts diff --git a/src/containers/LoginPage.ts b/frontend/src/containers/LoginPage.ts similarity index 100% rename from src/containers/LoginPage.ts rename to frontend/src/containers/LoginPage.ts diff --git a/src/containers/Review.ts b/frontend/src/containers/Review.ts similarity index 100% rename from src/containers/Review.ts rename to frontend/src/containers/Review.ts diff --git a/src/containers/SummaryPage.ts b/frontend/src/containers/SummaryPage.ts similarity index 100% rename from src/containers/SummaryPage.ts rename to frontend/src/containers/SummaryPage.ts diff --git a/src/index.css b/frontend/src/index.css similarity index 100% rename from src/index.css rename to frontend/src/index.css diff --git a/src/index.html b/frontend/src/index.html similarity index 100% rename from src/index.html rename to frontend/src/index.html diff --git a/src/index.tsx b/frontend/src/index.tsx similarity index 100% rename from src/index.tsx rename to frontend/src/index.tsx diff --git a/src/models/learner.ts b/frontend/src/models/learner.ts similarity index 100% rename from src/models/learner.ts rename to frontend/src/models/learner.ts diff --git a/src/models/level.ts b/frontend/src/models/level.ts similarity index 100% rename from src/models/level.ts rename to frontend/src/models/level.ts diff --git a/src/models/review.ts b/frontend/src/models/review.ts similarity index 100% rename from src/models/review.ts rename to frontend/src/models/review.ts diff --git a/src/models/user.ts b/frontend/src/models/user.ts similarity index 100% rename from src/models/user.ts rename to frontend/src/models/user.ts diff --git a/src/models/vocab.ts b/frontend/src/models/vocab.ts similarity index 100% rename from src/models/vocab.ts rename to frontend/src/models/vocab.ts diff --git a/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx similarity index 100% rename from src/pages/dashboard.tsx rename to frontend/src/pages/dashboard.tsx diff --git a/src/pages/intro.tsx b/frontend/src/pages/intro.tsx similarity index 100% rename from src/pages/intro.tsx rename to frontend/src/pages/intro.tsx diff --git a/src/pages/level.tsx b/frontend/src/pages/level.tsx similarity index 100% rename from src/pages/level.tsx rename to frontend/src/pages/level.tsx diff --git a/src/pages/levelList.tsx b/frontend/src/pages/levelList.tsx similarity index 100% rename from src/pages/levelList.tsx rename to frontend/src/pages/levelList.tsx diff --git a/src/pages/login.tsx b/frontend/src/pages/login.tsx similarity index 100% rename from src/pages/login.tsx rename to frontend/src/pages/login.tsx diff --git a/src/pages/review.tsx b/frontend/src/pages/review.tsx similarity index 100% rename from src/pages/review.tsx rename to frontend/src/pages/review.tsx diff --git a/src/pages/summary.tsx b/frontend/src/pages/summary.tsx similarity index 100% rename from src/pages/summary.tsx rename to frontend/src/pages/summary.tsx diff --git a/src/reducers/index.ts b/frontend/src/reducers/index.ts similarity index 100% rename from src/reducers/index.ts rename to frontend/src/reducers/index.ts diff --git a/src/security/AuthRoute.tsx b/frontend/src/security/AuthRoute.tsx similarity index 100% rename from src/security/AuthRoute.tsx rename to frontend/src/security/AuthRoute.tsx diff --git a/src/security/Token.ts b/frontend/src/security/Token.ts similarity index 100% rename from src/security/Token.ts rename to frontend/src/security/Token.ts diff --git a/src/utils/__test__/queue.test.ts b/frontend/src/utils/__test__/queue.test.ts similarity index 100% rename from src/utils/__test__/queue.test.ts rename to frontend/src/utils/__test__/queue.test.ts diff --git a/src/utils/date.ts b/frontend/src/utils/date.ts similarity index 100% rename from src/utils/date.ts rename to frontend/src/utils/date.ts diff --git a/src/utils/queue.ts b/frontend/src/utils/queue.ts similarity index 100% rename from src/utils/queue.ts rename to frontend/src/utils/queue.ts diff --git a/tsconfig.json b/frontend/tsconfig.json similarity index 100% rename from tsconfig.json rename to frontend/tsconfig.json