Add password reset form
This commit is contained in:
parent
14bb35d1bb
commit
3eee0c189e
61
src/back/routes/admin/password.form.ts
Normal file
61
src/back/routes/admin/password.form.ts
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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<typeof fields, void> {
|
||||||
|
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: <const>"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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})();
|
Loading…
x
Reference in New Issue
Block a user