From ecd8fe9328cb7cc62a824298f806d2e4b2521dfe Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 7 Feb 2021 18:07:27 +0100 Subject: [PATCH] Add login routes --- package-lock.json | 119 +++++++++++++++++++----------------- src/back/html.ts | 7 ++- src/back/index.ts | 2 + src/back/routes/homepage.ts | 13 ++-- src/back/routes/login.ts | 75 +++++++++++++++++++++++ 5 files changed, 154 insertions(+), 62 deletions(-) create mode 100644 src/back/routes/login.ts diff --git a/package-lock.json b/package-lock.json index a54604b..99a27f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -234,9 +234,9 @@ } }, "@babel/runtime": { - "version": "7.12.5", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.12.5.tgz", - "integrity": "sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg==", + "version": "7.12.13", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.12.13.tgz", + "integrity": "sha512-8+3UMPBrjFa/6TtKi/7sehPKqfAm4g6K+YQjyyFOLUTxzOngcRZTlAVY8sc2CORJYqdHQY8gRPHmn+qo15rCBw==", "requires": { "regenerator-runtime": "^0.13.4" } @@ -466,20 +466,20 @@ "dev": true }, "@types/express": { - "version": "4.17.9", - "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.9.tgz", - "integrity": "sha512-SDzEIZInC4sivGIFY4Sz1GG6J9UObPwCInYJjko2jzOf/Imx/dlpume6Xxwj1ORL82tBbmN4cPDIDkLbWHk9hw==", + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", + "integrity": "sha512-no+R6rW60JEc59977wIxreQVsIEOAYwgCqldrA/vkpCnbD7MqTefO97lmoBe4WE0F156bC4uLSP1XHDOySnChg==", "requires": { "@types/body-parser": "*", - "@types/express-serve-static-core": "*", + "@types/express-serve-static-core": "^4.17.18", "@types/qs": "*", "@types/serve-static": "*" } }, "@types/express-serve-static-core": { - "version": "4.17.17", - "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.17.tgz", - "integrity": "sha512-YYlVaCni5dnHc+bLZfY908IG1+x5xuibKZMGv8srKkvtul3wUuanYvpIj9GXXoWkQbaAdR+kgX46IETKUALWNQ==", + "version": "4.17.18", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.18.tgz", + "integrity": "sha512-m4JTwx5RUBNZvky/JJ8swEJPKFd8si08pPF2PfizYjGZOKr/svUWPcoUmLow6MmPzhasphB7gSTINY67xn3JNA==", "requires": { "@types/node": "*", "@types/qs": "*", @@ -515,9 +515,9 @@ "integrity": "sha512-GJhpTepz2udxGexqos8wgaBx4I/zWIDPh/KOGEwAqtuGDkOUJu5eFvwmdBX4AmB8Odsr+9pHCQqiAqDL/yKMKw==" }, "@types/koa": { - "version": "2.11.6", - "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.11.6.tgz", - "integrity": "sha512-BhyrMj06eQkk04C97fovEDQMpLpd2IxCB4ecitaXwOKGq78Wi2tooaDOWOFGajPk8IkQOAtMppApgSVkYe1F/A==", + "version": "2.11.7", + "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.11.7.tgz", + "integrity": "sha512-1iXJZZWCePoMe9LGSIPWsu5k5RI4ooXijW78c+nljMn3YbUts8PXoEESu1OeFmrazLPl1l97vTxzwvmH32TWVQ==", "requires": { "@types/accepts": "*", "@types/content-disposition": "*", @@ -598,9 +598,9 @@ "integrity": "sha512-ZvO2tAcjmMi8V/5Z3JsyofMe3hasRcaw88cto5etSVMwVQfeivGAlEYmaQgceUSVYFofVjT+ioHsATjdWcFt1w==" }, "@types/mongodb": { - "version": "3.6.3", - "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.3.tgz", - "integrity": "sha512-6YNqGP1hk5bjUFaim+QoFFuI61WjHiHE1BNeB41TA00Xd2K7zG4lcWyLLq/XtIp36uMavvS5hoAUJ+1u/GcX2Q==", + "version": "3.6.6", + "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.6.tgz", + "integrity": "sha512-ghYevKiSh/TGk2MAwSRZP7T1ilR9Pw8Fa7pT9GGVGZPUsWKdZjZ4G6LG3MqK2iXKdNba994F8W9ikA+qx2Eo3A==", "requires": { "@types/bson": "*", "@types/node": "*" @@ -635,12 +635,19 @@ "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" }, "@types/serve-static": { - "version": "1.13.8", - "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.8.tgz", - "integrity": "sha512-MoJhSQreaVoL+/hurAZzIm8wafFR6ajiTM1m4A0kv6AGeVBl4r4pOV8bGFrjjq1sGxDTnCoF8i22o0/aE5XCyA==", + "version": "1.13.9", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.9.tgz", + "integrity": "sha512-ZFqF6qa48XsPdjXV5Gsz0Zqmux2PerNd3a/ktL45mHpa19cuMi/cL8tcxdAx497yRh+QtYPuofjT9oWw9P7nkA==", "requires": { - "@types/mime": "*", + "@types/mime": "^1", "@types/node": "*" + }, + "dependencies": { + "@types/mime": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", + "integrity": "sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==" + } } }, "@types/sharp": { @@ -2943,9 +2950,9 @@ } }, "js-beautify": { - "version": "1.13.1", - "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.13.1.tgz", - "integrity": "sha512-RGc3cSmQR3xPzTtIFAMwHK0R84MwDNpxpZgoQGySGdzShfNjBjHXqz99kMFwXDGlMSFVJVlsUXBFfqKUzJbzUg==", + "version": "1.13.5", + "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.13.5.tgz", + "integrity": "sha512-MsXlH6Z/BiRYSkSRW3clNDqDjSpiSNOiG8xYVUBXt4k0LnGvDhlTGOlHX1VFtAdoLmtwjxMG5qiWKy/g+Ipv5w==", "requires": { "config-chain": "^1.1.12", "editorconfig": "^0.15.3", @@ -3338,9 +3345,9 @@ } }, "mime": { - "version": "2.4.7", - "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.7.tgz", - "integrity": "sha512-dhNd1uA2u397uQk3Nv5LM4lm93WYDUXFn3Fu291FJerns4jyTudqhIWe4W04YLy7Uk1tm1Ore04NpjRvQp/NPA==" + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.5.0.tgz", + "integrity": "sha512-ft3WayFSFUVBuJj7BMLKAQcSlItKtfjsKDDsii3rqFDAZ7t11zRe8ASw/GlmivGwVUYtwkQrxiGGpL6gFvB0ag==" }, "mime-db": { "version": "1.45.0", @@ -3709,9 +3716,9 @@ } }, "entities": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", - "integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", + "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==" }, "htmlparser2": { "version": "4.1.0", @@ -3816,9 +3823,9 @@ "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==" }, "mongodb": { - "version": "3.6.3", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.3.tgz", - "integrity": "sha512-rOZuR0QkodZiM+UbQE5kDsJykBqWi0CL4Ec2i1nrGrUI3KO11r6Fbxskqmq3JK2NH7aW4dcccBuUujAP0ERl5w==", + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.4.tgz", + "integrity": "sha512-Y+Ki9iXE9jI+n9bVtbTOOdK0B95d6wVGSucwtBkvQ+HIvVdTCfpVRp01FDC24uhC/Q2WXQ8Lpq3/zwtB5Op9Qw==", "requires": { "bl": "^2.2.1", "bson": "^1.1.4", @@ -4374,9 +4381,9 @@ } }, "qs": { - "version": "6.9.4", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.4.tgz", - "integrity": "sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ==" + "version": "6.9.6", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.6.tgz", + "integrity": "sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ==" }, "randombytes": { "version": "2.1.0", @@ -4677,9 +4684,9 @@ } }, "entities": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", - "integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", + "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==" }, "htmlparser2": { "version": "4.1.0", @@ -4715,9 +4722,9 @@ } }, "sealious": { - "version": "0.13.4", - "resolved": "https://registry.npmjs.org/sealious/-/sealious-0.13.4.tgz", - "integrity": "sha512-Z7efbevRU1ZoA+CYDYUvM0+MD8wh41E9SgRR8O+afadc2oURrXCMHQetcugxK90LSntCtHB4jxnKYNCesc/qGw==", + "version": "0.13.13", + "resolved": "https://registry.npmjs.org/sealious/-/sealious-0.13.13.tgz", + "integrity": "sha512-+fXBlnmBYzVHqyf8dpzi7o+J9y+T6b3V8ZNi2Ok7RBMYEFZKghVDS6IRaarlR3N/MyZi8JgTOYbMbXyeJkZLtQ==", "requires": { "@babel/polyfill": "^7.0.0", "@koa/router": "^10.0.0", @@ -4779,9 +4786,9 @@ }, "dependencies": { "@types/node": { - "version": "13.13.39", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.39.tgz", - "integrity": "sha512-wct+WgRTTkBm2R3vbrFOqyZM5w0g+D8KnhstG9463CJBVC3UVZHMToge7iMBR1vDl/I+NWFHUeK9X+JcF0rWKw==" + "version": "13.13.41", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.41.tgz", + "integrity": "sha512-qLT9IvHiXJfdrje9VmsLzun7cQ65obsBTmtU3EOnCSLFOoSHx1hpiRHoBnpdbyFqnzqdUUIv81JcEJQCB8un9g==" } } }, @@ -5102,9 +5109,9 @@ }, "dependencies": { "bl": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/bl/-/bl-4.0.3.tgz", - "integrity": "sha512-fs4G6/Hu4/EE+F75J8DuN/0IpQqNjAdC7aEQv7Qt8MHGUH7Ckv2MwTEEeN9QehD0pfIDkMI1bkHYkKy7xHyKIg==", + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.0.4.tgz", + "integrity": "sha512-7tdr4EpSd7jJ6tuQ21vu2ke8w7pNEstzj1O8wwq6sNNzO3UDi5MA8Gny/gquCj7r2C6fHudg8tKRGyjRgmvNxQ==", "requires": { "buffer": "^5.5.0", "inherits": "^2.0.4", @@ -5241,9 +5248,9 @@ "integrity": "sha512-LxhtAkPDTkVCMQjt2h6eBVY28KCjikZqZfMcC15YBeNjkgUpdCfBu5HoiOTDu86v6smE8yOjyEktJ8hlbANHQA==" }, "tsutils": { - "version": "3.19.1", - "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.19.1.tgz", - "integrity": "sha512-GEdoBf5XI324lu7ycad7s6laADfnAqCw6wLGI+knxvw9vsIYBaJfYdmeCEG3FMMUiSm3OGgNb+m6utsWf5h9Vw==", + "version": "3.20.0", + "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.20.0.tgz", + "integrity": "sha512-RYbuQuvkhuqVeXweWT3tJLKOEJ/UUw9GjNEZGWdrLLlM+611o1gwLHBpxoFJKKl25fLprp2eVthtKs5JOrNeXg==", "requires": { "tslib": "^1.8.1" } @@ -5310,9 +5317,9 @@ "dev": true }, "uglify-js": { - "version": "3.12.4", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.12.4.tgz", - "integrity": "sha512-L5i5jg/SHkEqzN18gQMTWsZk3KelRsfD1wUVNqtq0kzqWQqcJjyL8yc1o8hJgRrWqrAl2mUFbhfznEIoi7zi2A==" + "version": "3.12.7", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.12.7.tgz", + "integrity": "sha512-SIZhkoh+U/wjW+BHGhVwE9nt8tWJspncloBcFapkpGRwNPqcH8pzX36BXe3TPBjzHWPMUZotpCigak/udWNr1Q==" }, "undefsafe": { "version": "2.0.3", @@ -5521,9 +5528,9 @@ } }, "entities": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", - "integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", + "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==" }, "htmlparser2": { "version": "4.1.0", diff --git a/src/back/html.ts b/src/back/html.ts index 3f8f40d..79e0aa7 100644 --- a/src/back/html.ts +++ b/src/back/html.ts @@ -1,4 +1,9 @@ -export default function html(body: string): string { +import { Context } from "sealious"; + +export default async function html( + context: Context, + body: string +): Promise { return /* HTML */ ` diff --git a/src/back/index.ts b/src/back/index.ts index 111d86f..283698e 100644 --- a/src/back/index.ts +++ b/src/back/index.ts @@ -3,6 +3,7 @@ import Sealious from "sealious"; import TheApp from "./app"; import homepage from "./routes/homepage"; import tasks from "./routes/tasks"; +import login from "./routes/login"; const locreq = _locreq(__dirname); declare module "koa" { @@ -19,5 +20,6 @@ app.start(); const router = app.HTTPServer.router; router.use("/", homepage.routes()); router.use("/tasks", tasks.routes()); +router.use("/login", login.routes()); app.HTTPServer.addStaticRoute("/", locreq.resolve("public")); diff --git a/src/back/routes/homepage.ts b/src/back/routes/homepage.ts index bf15c37..0512990 100644 --- a/src/back/routes/homepage.ts +++ b/src/back/routes/homepage.ts @@ -6,11 +6,14 @@ import { NewTask, TaskList } from "../views/tasks"; const router = new Router(); export async function MainView(context: Context) { - return html(/* HTML */ `My ToDo App - -

My ToDo App

- ${await TaskList(context)} ${NewTask()} - `); + return await html( + context, + /* HTML */ `My ToDo App + +

My ToDo App

+ ${await TaskList(context)} ${NewTask()} + ` + ); } router.get("/", Middlewares.extractContext(), async (ctx) => { diff --git a/src/back/routes/login.ts b/src/back/routes/login.ts new file mode 100644 index 0000000..bd738f5 --- /dev/null +++ b/src/back/routes/login.ts @@ -0,0 +1,75 @@ +import Router from "@koa/router"; +import { Middlewares } from "sealious"; + +import html from "../html"; + +const router = new Router(); + +router.get("/", Middlewares.extractContext(), async (ctx) => { + ctx.body = await html(ctx.$context, LoginForm()); +}); + +router.post( + "/", + Middlewares.extractContext(), + Middlewares.parseBody(), + async (ctx) => { + try { + const session_id = await ctx.$app.collections.sessions.login( + ctx.$body.username as string, + ctx.$body.password as string + ); + ctx.cookies.set("sealious-session", session_id, { + maxAge: 1000 * 60 * 60 * 24 * 7, + secure: ctx.request.protocol === "https", + overwrite: true, + }); + ctx.redirect("/user"); + } catch (e) { + ctx.status = 422; + ctx.body = await html( + ctx.$context, + LoginForm(ctx.$body.username as string, (e as Error).message) + ); + } + } +); + +export default router; + +function LoginForm(username = "", error_message?: string): string { + if (error_message) { + error_message = + error_message == "Incorrect username!" + ? "Niepoprawna nazwa użytkownika!" + : "Niepoprawne hasło!"; + } + return /* HTML */ ` + +

Zaloguj

+
+ ${error_message ? `
${error_message}
` : ""} + + + +
+
+ `; +}