Add login routes

master
Kuba Orlik 4 years ago
parent 93d6546ce9
commit ecd8fe9328

119
package-lock.json generated

@ -234,9 +234,9 @@
} }
}, },
"@babel/runtime": { "@babel/runtime": {
"version": "7.12.5", "version": "7.12.13",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.12.5.tgz", "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.12.13.tgz",
"integrity": "sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg==", "integrity": "sha512-8+3UMPBrjFa/6TtKi/7sehPKqfAm4g6K+YQjyyFOLUTxzOngcRZTlAVY8sc2CORJYqdHQY8gRPHmn+qo15rCBw==",
"requires": { "requires": {
"regenerator-runtime": "^0.13.4" "regenerator-runtime": "^0.13.4"
} }
@ -466,20 +466,20 @@
"dev": true "dev": true
}, },
"@types/express": { "@types/express": {
"version": "4.17.9", "version": "4.17.11",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.9.tgz", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz",
"integrity": "sha512-SDzEIZInC4sivGIFY4Sz1GG6J9UObPwCInYJjko2jzOf/Imx/dlpume6Xxwj1ORL82tBbmN4cPDIDkLbWHk9hw==", "integrity": "sha512-no+R6rW60JEc59977wIxreQVsIEOAYwgCqldrA/vkpCnbD7MqTefO97lmoBe4WE0F156bC4uLSP1XHDOySnChg==",
"requires": { "requires": {
"@types/body-parser": "*", "@types/body-parser": "*",
"@types/express-serve-static-core": "*", "@types/express-serve-static-core": "^4.17.18",
"@types/qs": "*", "@types/qs": "*",
"@types/serve-static": "*" "@types/serve-static": "*"
} }
}, },
"@types/express-serve-static-core": { "@types/express-serve-static-core": {
"version": "4.17.17", "version": "4.17.18",
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.17.tgz", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.18.tgz",
"integrity": "sha512-YYlVaCni5dnHc+bLZfY908IG1+x5xuibKZMGv8srKkvtul3wUuanYvpIj9GXXoWkQbaAdR+kgX46IETKUALWNQ==", "integrity": "sha512-m4JTwx5RUBNZvky/JJ8swEJPKFd8si08pPF2PfizYjGZOKr/svUWPcoUmLow6MmPzhasphB7gSTINY67xn3JNA==",
"requires": { "requires": {
"@types/node": "*", "@types/node": "*",
"@types/qs": "*", "@types/qs": "*",
@ -515,9 +515,9 @@
"integrity": "sha512-GJhpTepz2udxGexqos8wgaBx4I/zWIDPh/KOGEwAqtuGDkOUJu5eFvwmdBX4AmB8Odsr+9pHCQqiAqDL/yKMKw==" "integrity": "sha512-GJhpTepz2udxGexqos8wgaBx4I/zWIDPh/KOGEwAqtuGDkOUJu5eFvwmdBX4AmB8Odsr+9pHCQqiAqDL/yKMKw=="
}, },
"@types/koa": { "@types/koa": {
"version": "2.11.6", "version": "2.11.7",
"resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.11.6.tgz", "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.11.7.tgz",
"integrity": "sha512-BhyrMj06eQkk04C97fovEDQMpLpd2IxCB4ecitaXwOKGq78Wi2tooaDOWOFGajPk8IkQOAtMppApgSVkYe1F/A==", "integrity": "sha512-1iXJZZWCePoMe9LGSIPWsu5k5RI4ooXijW78c+nljMn3YbUts8PXoEESu1OeFmrazLPl1l97vTxzwvmH32TWVQ==",
"requires": { "requires": {
"@types/accepts": "*", "@types/accepts": "*",
"@types/content-disposition": "*", "@types/content-disposition": "*",
@ -598,9 +598,9 @@
"integrity": "sha512-ZvO2tAcjmMi8V/5Z3JsyofMe3hasRcaw88cto5etSVMwVQfeivGAlEYmaQgceUSVYFofVjT+ioHsATjdWcFt1w==" "integrity": "sha512-ZvO2tAcjmMi8V/5Z3JsyofMe3hasRcaw88cto5etSVMwVQfeivGAlEYmaQgceUSVYFofVjT+ioHsATjdWcFt1w=="
}, },
"@types/mongodb": { "@types/mongodb": {
"version": "3.6.3", "version": "3.6.6",
"resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.3.tgz", "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.6.tgz",
"integrity": "sha512-6YNqGP1hk5bjUFaim+QoFFuI61WjHiHE1BNeB41TA00Xd2K7zG4lcWyLLq/XtIp36uMavvS5hoAUJ+1u/GcX2Q==", "integrity": "sha512-ghYevKiSh/TGk2MAwSRZP7T1ilR9Pw8Fa7pT9GGVGZPUsWKdZjZ4G6LG3MqK2iXKdNba994F8W9ikA+qx2Eo3A==",
"requires": { "requires": {
"@types/bson": "*", "@types/bson": "*",
"@types/node": "*" "@types/node": "*"
@ -635,12 +635,19 @@
"integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA=="
}, },
"@types/serve-static": { "@types/serve-static": {
"version": "1.13.8", "version": "1.13.9",
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.8.tgz", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.9.tgz",
"integrity": "sha512-MoJhSQreaVoL+/hurAZzIm8wafFR6ajiTM1m4A0kv6AGeVBl4r4pOV8bGFrjjq1sGxDTnCoF8i22o0/aE5XCyA==", "integrity": "sha512-ZFqF6qa48XsPdjXV5Gsz0Zqmux2PerNd3a/ktL45mHpa19cuMi/cL8tcxdAx497yRh+QtYPuofjT9oWw9P7nkA==",
"requires": { "requires": {
"@types/mime": "*", "@types/mime": "^1",
"@types/node": "*" "@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": { "@types/sharp": {
@ -2943,9 +2950,9 @@
} }
}, },
"js-beautify": { "js-beautify": {
"version": "1.13.1", "version": "1.13.5",
"resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.13.1.tgz", "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.13.5.tgz",
"integrity": "sha512-RGc3cSmQR3xPzTtIFAMwHK0R84MwDNpxpZgoQGySGdzShfNjBjHXqz99kMFwXDGlMSFVJVlsUXBFfqKUzJbzUg==", "integrity": "sha512-MsXlH6Z/BiRYSkSRW3clNDqDjSpiSNOiG8xYVUBXt4k0LnGvDhlTGOlHX1VFtAdoLmtwjxMG5qiWKy/g+Ipv5w==",
"requires": { "requires": {
"config-chain": "^1.1.12", "config-chain": "^1.1.12",
"editorconfig": "^0.15.3", "editorconfig": "^0.15.3",
@ -3338,9 +3345,9 @@
} }
}, },
"mime": { "mime": {
"version": "2.4.7", "version": "2.5.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-2.4.7.tgz", "resolved": "https://registry.npmjs.org/mime/-/mime-2.5.0.tgz",
"integrity": "sha512-dhNd1uA2u397uQk3Nv5LM4lm93WYDUXFn3Fu291FJerns4jyTudqhIWe4W04YLy7Uk1tm1Ore04NpjRvQp/NPA==" "integrity": "sha512-ft3WayFSFUVBuJj7BMLKAQcSlItKtfjsKDDsii3rqFDAZ7t11zRe8ASw/GlmivGwVUYtwkQrxiGGpL6gFvB0ag=="
}, },
"mime-db": { "mime-db": {
"version": "1.45.0", "version": "1.45.0",
@ -3709,9 +3716,9 @@
} }
}, },
"entities": { "entities": {
"version": "2.1.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz",
"integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==" "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A=="
}, },
"htmlparser2": { "htmlparser2": {
"version": "4.1.0", "version": "4.1.0",
@ -3816,9 +3823,9 @@
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==" "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A=="
}, },
"mongodb": { "mongodb": {
"version": "3.6.3", "version": "3.6.4",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.3.tgz", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.4.tgz",
"integrity": "sha512-rOZuR0QkodZiM+UbQE5kDsJykBqWi0CL4Ec2i1nrGrUI3KO11r6Fbxskqmq3JK2NH7aW4dcccBuUujAP0ERl5w==", "integrity": "sha512-Y+Ki9iXE9jI+n9bVtbTOOdK0B95d6wVGSucwtBkvQ+HIvVdTCfpVRp01FDC24uhC/Q2WXQ8Lpq3/zwtB5Op9Qw==",
"requires": { "requires": {
"bl": "^2.2.1", "bl": "^2.2.1",
"bson": "^1.1.4", "bson": "^1.1.4",
@ -4374,9 +4381,9 @@
} }
}, },
"qs": { "qs": {
"version": "6.9.4", "version": "6.9.6",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.4.tgz", "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.6.tgz",
"integrity": "sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ==" "integrity": "sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ=="
}, },
"randombytes": { "randombytes": {
"version": "2.1.0", "version": "2.1.0",
@ -4677,9 +4684,9 @@
} }
}, },
"entities": { "entities": {
"version": "2.1.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz",
"integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==" "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A=="
}, },
"htmlparser2": { "htmlparser2": {
"version": "4.1.0", "version": "4.1.0",
@ -4715,9 +4722,9 @@
} }
}, },
"sealious": { "sealious": {
"version": "0.13.4", "version": "0.13.13",
"resolved": "https://registry.npmjs.org/sealious/-/sealious-0.13.4.tgz", "resolved": "https://registry.npmjs.org/sealious/-/sealious-0.13.13.tgz",
"integrity": "sha512-Z7efbevRU1ZoA+CYDYUvM0+MD8wh41E9SgRR8O+afadc2oURrXCMHQetcugxK90LSntCtHB4jxnKYNCesc/qGw==", "integrity": "sha512-+fXBlnmBYzVHqyf8dpzi7o+J9y+T6b3V8ZNi2Ok7RBMYEFZKghVDS6IRaarlR3N/MyZi8JgTOYbMbXyeJkZLtQ==",
"requires": { "requires": {
"@babel/polyfill": "^7.0.0", "@babel/polyfill": "^7.0.0",
"@koa/router": "^10.0.0", "@koa/router": "^10.0.0",
@ -4779,9 +4786,9 @@
}, },
"dependencies": { "dependencies": {
"@types/node": { "@types/node": {
"version": "13.13.39", "version": "13.13.41",
"resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.39.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.41.tgz",
"integrity": "sha512-wct+WgRTTkBm2R3vbrFOqyZM5w0g+D8KnhstG9463CJBVC3UVZHMToge7iMBR1vDl/I+NWFHUeK9X+JcF0rWKw==" "integrity": "sha512-qLT9IvHiXJfdrje9VmsLzun7cQ65obsBTmtU3EOnCSLFOoSHx1hpiRHoBnpdbyFqnzqdUUIv81JcEJQCB8un9g=="
} }
} }
}, },
@ -5102,9 +5109,9 @@
}, },
"dependencies": { "dependencies": {
"bl": { "bl": {
"version": "4.0.3", "version": "4.0.4",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.0.3.tgz", "resolved": "https://registry.npmjs.org/bl/-/bl-4.0.4.tgz",
"integrity": "sha512-fs4G6/Hu4/EE+F75J8DuN/0IpQqNjAdC7aEQv7Qt8MHGUH7Ckv2MwTEEeN9QehD0pfIDkMI1bkHYkKy7xHyKIg==", "integrity": "sha512-7tdr4EpSd7jJ6tuQ21vu2ke8w7pNEstzj1O8wwq6sNNzO3UDi5MA8Gny/gquCj7r2C6fHudg8tKRGyjRgmvNxQ==",
"requires": { "requires": {
"buffer": "^5.5.0", "buffer": "^5.5.0",
"inherits": "^2.0.4", "inherits": "^2.0.4",
@ -5241,9 +5248,9 @@
"integrity": "sha512-LxhtAkPDTkVCMQjt2h6eBVY28KCjikZqZfMcC15YBeNjkgUpdCfBu5HoiOTDu86v6smE8yOjyEktJ8hlbANHQA==" "integrity": "sha512-LxhtAkPDTkVCMQjt2h6eBVY28KCjikZqZfMcC15YBeNjkgUpdCfBu5HoiOTDu86v6smE8yOjyEktJ8hlbANHQA=="
}, },
"tsutils": { "tsutils": {
"version": "3.19.1", "version": "3.20.0",
"resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.19.1.tgz", "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.20.0.tgz",
"integrity": "sha512-GEdoBf5XI324lu7ycad7s6laADfnAqCw6wLGI+knxvw9vsIYBaJfYdmeCEG3FMMUiSm3OGgNb+m6utsWf5h9Vw==", "integrity": "sha512-RYbuQuvkhuqVeXweWT3tJLKOEJ/UUw9GjNEZGWdrLLlM+611o1gwLHBpxoFJKKl25fLprp2eVthtKs5JOrNeXg==",
"requires": { "requires": {
"tslib": "^1.8.1" "tslib": "^1.8.1"
} }
@ -5310,9 +5317,9 @@
"dev": true "dev": true
}, },
"uglify-js": { "uglify-js": {
"version": "3.12.4", "version": "3.12.7",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.12.4.tgz", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.12.7.tgz",
"integrity": "sha512-L5i5jg/SHkEqzN18gQMTWsZk3KelRsfD1wUVNqtq0kzqWQqcJjyL8yc1o8hJgRrWqrAl2mUFbhfznEIoi7zi2A==" "integrity": "sha512-SIZhkoh+U/wjW+BHGhVwE9nt8tWJspncloBcFapkpGRwNPqcH8pzX36BXe3TPBjzHWPMUZotpCigak/udWNr1Q=="
}, },
"undefsafe": { "undefsafe": {
"version": "2.0.3", "version": "2.0.3",
@ -5521,9 +5528,9 @@
} }
}, },
"entities": { "entities": {
"version": "2.1.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz",
"integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==" "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A=="
}, },
"htmlparser2": { "htmlparser2": {
"version": "4.1.0", "version": "4.1.0",

@ -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<string> {
return /* HTML */ `<!DOCTYPE html> return /* HTML */ `<!DOCTYPE html>
<html> <html>
<head> <head>

@ -3,6 +3,7 @@ import Sealious from "sealious";
import TheApp from "./app"; import TheApp from "./app";
import homepage from "./routes/homepage"; import homepage from "./routes/homepage";
import tasks from "./routes/tasks"; import tasks from "./routes/tasks";
import login from "./routes/login";
const locreq = _locreq(__dirname); const locreq = _locreq(__dirname);
declare module "koa" { declare module "koa" {
@ -19,5 +20,6 @@ app.start();
const router = app.HTTPServer.router; const router = app.HTTPServer.router;
router.use("/", homepage.routes()); router.use("/", homepage.routes());
router.use("/tasks", tasks.routes()); router.use("/tasks", tasks.routes());
router.use("/login", login.routes());
app.HTTPServer.addStaticRoute("/", locreq.resolve("public")); app.HTTPServer.addStaticRoute("/", locreq.resolve("public"));

@ -6,11 +6,14 @@ import { NewTask, TaskList } from "../views/tasks";
const router = new Router(); const router = new Router();
export async function MainView(context: Context) { export async function MainView(context: Context) {
return html(/* HTML */ `<title>My ToDo App</title> return await html(
context,
/* HTML */ `<title>My ToDo App</title>
<body> <body>
<h1>My ToDo App</h1> <h1>My ToDo App</h1>
${await TaskList(context)} ${NewTask()} ${await TaskList(context)} ${NewTask()}
</body>`); </body>`
);
} }
router.get("/", Middlewares.extractContext(), async (ctx) => { router.get("/", Middlewares.extractContext(), async (ctx) => {

@ -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 */ `
<turbo-frame id="login">
<h2>Zaloguj</h2>
<form method="POST" action="/login" data-turbo-frame="_top">
${error_message ? `<div>${error_message}</div>` : ""}
<label for="username">
Nazwa użytkownika:
<input
id="username"
name="username"
type="text"
value="${username}"
required
/>
</label>
<label for="password"
>Hasło:
<input
id="password"
name="password"
type="password"
value=""
required
/></label>
<input type="submit" value="Zaloguj" />
</form>
</turbo-frame>
`;
}
Loading…
Cancel
Save