2023-02-16 22:39:57 +00:00
|
|
|
import type { ResetPasswordRequest } from "@prisma/client";
|
|
|
|
import type { GetServerSidePropsContext } from "next";
|
2022-01-07 20:23:37 +00:00
|
|
|
import { getCsrfToken } from "next-auth/react";
|
2023-03-09 09:14:04 +00:00
|
|
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
2021-09-22 19:52:38 +00:00
|
|
|
import Link from "next/link";
|
2023-04-13 10:59:32 +00:00
|
|
|
import type { CSSProperties } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
import React, { useMemo } from "react";
|
2023-07-10 19:13:20 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-02-14 15:01:34 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-06-28 20:40:58 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2023-07-10 19:13:20 +00:00
|
|
|
import { Button, PasswordField, Form } from "@calcom/ui";
|
2022-06-28 20:40:58 +00:00
|
|
|
|
2023-04-18 18:45:32 +00:00
|
|
|
import PageWrapper from "@components/PageWrapper";
|
2022-11-01 13:29:01 +00:00
|
|
|
import AuthContainer from "@components/ui/AuthContainer";
|
2021-06-24 15:59:11 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
id: string;
|
|
|
|
resetPasswordRequest: ResetPasswordRequest;
|
|
|
|
csrfToken: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function Page({ resetPasswordRequest, csrfToken }: Props) {
|
2021-10-14 14:24:21 +00:00
|
|
|
const { t } = useLocale();
|
2023-07-10 19:13:20 +00:00
|
|
|
const formMethods = useForm<{ new_password: string }>();
|
|
|
|
const success = formMethods.formState.isSubmitSuccessful;
|
|
|
|
const loading = formMethods.formState.isSubmitting;
|
2021-06-24 15:59:11 +00:00
|
|
|
|
2022-01-07 20:23:37 +00:00
|
|
|
const submitChangePassword = async ({ password, requestId }: { password: string; requestId: string }) => {
|
2023-07-10 19:13:20 +00:00
|
|
|
const res = await fetch("/api/auth/reset-password", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ requestId, password }),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const json = await res.json();
|
|
|
|
if (!res.ok) return formMethods.setError("new_password", { type: "server", message: json.message });
|
2021-06-24 15:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Success = () => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="space-y-6">
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<h2 className="font-cal text-emphasis mt-6 text-center text-3xl font-extrabold">
|
2022-09-12 09:25:54 +00:00
|
|
|
{t("password_updated")}
|
2021-10-14 14:24:21 +00:00
|
|
|
</h2>
|
2021-06-24 15:59:11 +00:00
|
|
|
</div>
|
2022-09-12 09:25:54 +00:00
|
|
|
<Button href="/auth/login" className="w-full justify-center">
|
|
|
|
{t("login")}
|
|
|
|
</Button>
|
2021-06-24 15:59:11 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Expired = () => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="space-y-6">
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<h2 className="font-cal text-emphasis mt-6 text-center text-3xl font-extrabold">{t("whoops")}</h2>
|
|
|
|
<h2 className="text-emphasis text-center text-3xl font-extrabold">{t("request_is_expired")}</h2>
|
2021-06-24 15:59:11 +00:00
|
|
|
</div>
|
2021-10-14 14:24:21 +00:00
|
|
|
<p>{t("request_is_expired_instructions")}</p>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link href="/auth/forgot-password" passHref legacyBehavior>
|
2021-06-24 15:59:11 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
2022-02-09 00:05:13 +00:00
|
|
|
className="flex w-full justify-center px-4 py-2 text-sm font-medium text-blue-600 focus:outline-none focus:ring-2 focus:ring-black focus:ring-offset-2">
|
2021-10-14 14:24:21 +00:00
|
|
|
{t("try_again")}
|
2021-06-24 15:59:11 +00:00
|
|
|
</button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isRequestExpired = useMemo(() => {
|
|
|
|
const now = dayjs();
|
|
|
|
return dayjs(resetPasswordRequest.expires).isBefore(now);
|
|
|
|
}, [resetPasswordRequest]);
|
|
|
|
|
|
|
|
return (
|
2022-09-12 09:25:54 +00:00
|
|
|
<AuthContainer
|
|
|
|
showLogo
|
|
|
|
title={t("reset_password")}
|
|
|
|
description={t("change_your_password")}
|
|
|
|
heading={!success ? t("reset_password") : undefined}>
|
|
|
|
{isRequestExpired && <Expired />}
|
|
|
|
{!isRequestExpired && !success && (
|
|
|
|
<>
|
2023-07-10 19:13:20 +00:00
|
|
|
<Form
|
2022-09-12 09:25:54 +00:00
|
|
|
className="space-y-6"
|
2023-07-10 19:13:20 +00:00
|
|
|
form={formMethods}
|
2023-04-13 10:59:32 +00:00
|
|
|
style={
|
|
|
|
{
|
|
|
|
"--cal-brand": "#111827",
|
|
|
|
"--cal-brand-emphasis": "#101010",
|
|
|
|
"--cal-brand-text": "white",
|
|
|
|
"--cal-brand-subtle": "#9CA3AF",
|
|
|
|
} as CSSProperties
|
|
|
|
}
|
2023-07-10 19:13:20 +00:00
|
|
|
handleSubmit={async (values) => {
|
|
|
|
await submitChangePassword({
|
|
|
|
password: values.new_password,
|
|
|
|
requestId: resetPasswordRequest.id,
|
|
|
|
});
|
|
|
|
}}>
|
2022-09-12 09:25:54 +00:00
|
|
|
<input name="csrfToken" type="hidden" defaultValue={csrfToken} hidden />
|
|
|
|
<div className="mt-1">
|
2023-07-10 19:13:20 +00:00
|
|
|
<PasswordField
|
|
|
|
{...formMethods.register("new_password", {
|
|
|
|
minLength: {
|
|
|
|
message: t("password_hint_min"),
|
|
|
|
value: 7, // We don't have user here so we can't check if they are admin or not
|
|
|
|
},
|
|
|
|
pattern: {
|
|
|
|
message: "Should contain a number, uppercase and lowercase letters",
|
|
|
|
value: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).*$/gm,
|
|
|
|
},
|
|
|
|
})}
|
2022-09-12 09:25:54 +00:00
|
|
|
label={t("new_password")}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
loading={loading}
|
|
|
|
color="primary"
|
|
|
|
type="submit"
|
|
|
|
disabled={loading}
|
|
|
|
className="w-full justify-center">
|
|
|
|
{t("reset_password")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2023-07-10 19:13:20 +00:00
|
|
|
</Form>
|
2022-09-12 09:25:54 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{!isRequestExpired && success && (
|
|
|
|
<>
|
|
|
|
<Success />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</AuthContainer>
|
2021-06-24 15:59:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:59:32 +00:00
|
|
|
Page.isThemeSupported = false;
|
2023-04-18 18:45:32 +00:00
|
|
|
Page.PageWrapper = PageWrapper;
|
2021-06-24 15:59:11 +00:00
|
|
|
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
2022-01-07 20:23:37 +00:00
|
|
|
const id = context.params?.id as string;
|
2021-06-24 15:59:11 +00:00
|
|
|
|
|
|
|
try {
|
2022-08-31 19:44:47 +00:00
|
|
|
const resetPasswordRequest = await prisma.resetPasswordRequest.findUniqueOrThrow({
|
2021-06-24 15:59:11 +00:00
|
|
|
where: {
|
2022-01-07 20:23:37 +00:00
|
|
|
id,
|
2021-06-24 15:59:11 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
expires: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
resetPasswordRequest: {
|
|
|
|
...resetPasswordRequest,
|
|
|
|
expires: resetPasswordRequest.expires.toString(),
|
|
|
|
},
|
|
|
|
id,
|
|
|
|
csrfToken: await getCsrfToken({ req: context.req }),
|
2023-03-09 09:14:04 +00:00
|
|
|
...(await serverSideTranslations(context.locale || "en", ["common"])),
|
2021-06-24 15:59:11 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (reason) {
|
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|