shellphone.app/app/auth/pages/reset-password.tsx

63 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-08-01 03:05:40 +00:00
import type { BlitzPage } from "blitz";
import { useRouterQuery, Link, useMutation, Routes } from "blitz";
2021-07-31 14:33:18 +00:00
import BaseLayout from "../../core/layouts/base-layout";
import { LabeledTextField } from "../../core/components/labeled-text-field";
import { Form, FORM_ERROR } from "../../core/components/form";
import { ResetPassword } from "../validations";
import resetPassword from "../../auth/mutations/reset-password";
2021-07-31 14:33:18 +00:00
const ResetPasswordPage: BlitzPage = () => {
const query = useRouterQuery();
const [resetPasswordMutation, { isSuccess }] = useMutation(resetPassword);
2021-07-31 14:33:18 +00:00
return (
<div>
<h1>Set a New Password</h1>
{isSuccess ? (
<div>
<h2>Password Reset Successfully</h2>
<p>
2021-08-26 19:17:46 +00:00
Go to the <Link href={Routes.LandingPage()}>homepage</Link>
2021-07-31 14:33:18 +00:00
</p>
</div>
) : (
<Form
submitText="Reset Password"
schema={ResetPassword}
initialValues={{
password: "",
passwordConfirmation: "",
token: query.token as string,
}}
onSubmit={async (values) => {
try {
await resetPasswordMutation(values);
2021-07-31 14:33:18 +00:00
} catch (error) {
if (error.name === "ResetPasswordError") {
return {
[FORM_ERROR]: error.message,
};
2021-07-31 14:33:18 +00:00
} else {
return {
2021-08-01 14:03:49 +00:00
[FORM_ERROR]: "Sorry, we had an unexpected error. Please try again.",
};
2021-07-31 14:33:18 +00:00
}
}
}}
>
<LabeledTextField name="password" label="New Password" type="password" />
2021-08-01 14:03:49 +00:00
<LabeledTextField name="passwordConfirmation" label="Confirm New Password" type="password" />
2021-07-31 14:33:18 +00:00
</Form>
)}
</div>
);
};
2021-07-31 14:33:18 +00:00
2021-08-01 03:05:40 +00:00
ResetPasswordPage.redirectAuthenticatedTo = Routes.Messages();
ResetPasswordPage.getLayout = (page) => <BaseLayout title="Reset Your Password">{page}</BaseLayout>;
2021-07-31 14:33:18 +00:00
export default ResetPasswordPage;