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"; export const actionName = "ResetPassword"; const fields = { password: new Fields.SimpleFormField(true), }; export const ResetPasswordShape = fieldsToShape(fields); export default new (class ResetPasswordForm extends Form { defaultSuccessMessage = "Formularz wypełniony poprawnie"; fields = fields; controls = [ new Controls.SimpleInput(fields.password, { label: "Password:", type: "password", }), ]; async onSubmit(ctx: Context) { const { password } = await this.getParsedValues(ctx); const user_id = ctx.$context.user_id; if (!user_id) { throw new Error("Not logged in"); } const user = await ctx.$app.collections.users.getByID(ctx.$context, user_id); user.set("password", password); await user.save(new ctx.$app.SuperContext()); return; } async onSuccess() { return { action: "stay", content: "Password changed", messages: [], }; } // eslint-disable-next-line @typescript-eslint/no-unused-vars async canAccess(ctx: Context) { return { canAccess: !!ctx.$context.user_id, message: "Not logged in" }; } async render(ctx: Context, data: FormData, show_field_errors: boolean) { return html({ ctx, title: "Form", description: "", css_clumps: ["admin-forms"], body: (await super.render(ctx, data, show_field_errors)) as string, }); } })();