Add default login form
This commit is contained in:
parent
5b61410b17
commit
9fbf03dc83
68
src/back/routes/login.form.ts
Normal file
68
src/back/routes/login.form.ts
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/consistent-type-assertions */
|
||||||
|
import type { Context } from "koa";
|
||||||
|
import type { FormData } from "@sealcode/sealgen";
|
||||||
|
import { Form, Controls, fieldsToShape } from "@sealcode/sealgen";
|
||||||
|
import html from "../html.js";
|
||||||
|
|
||||||
|
import { Fields } from "@sealcode/sealgen";
|
||||||
|
import { hasShape, predicates } from "@sealcode/ts-predicates";
|
||||||
|
|
||||||
|
export const actionName = "Login";
|
||||||
|
|
||||||
|
const fields = {
|
||||||
|
username: new Fields.SimpleFormField(true),
|
||||||
|
password: new Fields.SimpleFormField(true),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const LoginShape = fieldsToShape(fields);
|
||||||
|
|
||||||
|
export default new (class LoginForm extends Form<typeof fields, void> {
|
||||||
|
defaultSuccessMessage = "Formularz wypełniony poprawnie";
|
||||||
|
fields = fields;
|
||||||
|
|
||||||
|
controls = [
|
||||||
|
new Controls.SimpleInput(fields.username, {
|
||||||
|
label: "Username:",
|
||||||
|
type: "text",
|
||||||
|
}),
|
||||||
|
new Controls.SimpleInput(fields.password, {
|
||||||
|
label: "Password:",
|
||||||
|
type: "password",
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
async canAccess(_: Context) {
|
||||||
|
return { canAccess: true, message: "" };
|
||||||
|
}
|
||||||
|
|
||||||
|
async onSubmit(ctx: Context) {
|
||||||
|
const body = ctx.$body;
|
||||||
|
console.log(body);
|
||||||
|
if (
|
||||||
|
!hasShape({ username: predicates.string, password: predicates.string }, body)
|
||||||
|
) {
|
||||||
|
throw new Error("Missing username or password");
|
||||||
|
}
|
||||||
|
const session_id = await ctx.$app.collections.sessions.login(
|
||||||
|
body.username,
|
||||||
|
body.password
|
||||||
|
);
|
||||||
|
ctx.cookies.set("sealious-session", session_id, {
|
||||||
|
maxAge: 1000 * 60 * 60 * 24 * 7,
|
||||||
|
secure: ctx.request.protocol === "https",
|
||||||
|
overwrite: true,
|
||||||
|
});
|
||||||
|
ctx.redirect("/user");
|
||||||
|
ctx.status = 303;
|
||||||
|
}
|
||||||
|
|
||||||
|
async render(ctx: Context, data: FormData, show_field_errors: boolean) {
|
||||||
|
return html({
|
||||||
|
ctx,
|
||||||
|
title: "Form",
|
||||||
|
description: "",
|
||||||
|
body: super.render(ctx, data, show_field_errors) as Promise<string>,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})();
|
Loading…
x
Reference in New Issue
Block a user