import { state, property } from "lit/decorators.js";
import { msg, localized } from "@lit/localize";
import type { ViewState } from "../utils/APIRouter";
import LiteElement, { html } from "../utils/LiteElement";
@localized()
export class ResetPassword extends LiteElement {
@property({ type: Object })
viewState!: ViewState;
@state()
private serverError?: string;
@state()
private isSubmitting: boolean = false;
render() {
let formError;
if (this.serverError) {
formError = html`
${this.serverError}
`;
}
return html`
${formError}
${msg("Change password")}
`;
}
async onSubmit(event: { detail: { formData: FormData } }) {
this.isSubmitting = true;
const { formData } = event.detail;
const password = formData.get("password") as string;
const resp = await fetch("/api/auth/reset-password", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: this.viewState.params.token,
password,
}),
});
switch (resp.status) {
case 200:
// TODO show toast notification
this.navTo("/log-in");
break;
case 400:
case 422:
const { detail } = await resp.json();
if (detail === "RESET_PASSWORD_BAD_TOKEN") {
// TODO password validation details
this.serverError = msg(
"Password reset email is not valid. Request a new password reset email"
);
} else {
// TODO password validation details
this.serverError = msg("Invalid password");
}
break;
default:
this.serverError = msg("Something unexpected went wrong");
break;
}
this.isSubmitting = false;
}
}