Skip to content

Commit

Permalink
Merge pull request #17 from bunsy-0900/bug/ES-396
Browse files Browse the repository at this point in the history
ES-696: confirm users before they reload the page
  • Loading branch information
aranaravi authored Jan 22, 2024
2 parents 22c6bca + 7deb8f3 commit b63bb8e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
24 changes: 18 additions & 6 deletions signup-ui/src/pages/ResetPasswordPage/ResetPasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo } from "react";
import { yupResolver } from "@hookform/resolvers/yup";
import { isEqual } from "lodash";
import { Resolver, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as yup from "yup";
Expand Down Expand Up @@ -69,7 +70,12 @@ export const ResetPasswordPage = ({ settings }: ResetPasswordPageProps) => {
// Step 3 - ResetPassword
yup.object({
newPassword: validatePassword(settings, t),
confirmNewPassword: validateConfirmPassword("newPassword", settings, t, false),
confirmNewPassword: validateConfirmPassword(
"newPassword",
settings,
t,
false
),
}),
// Step 4 - ResetPasswordStatus
yup.object({}),
Expand All @@ -92,13 +98,19 @@ export const ResetPasswordPage = ({ settings }: ResetPasswordPageProps) => {
});

const {
getValues,
formState: { isDirty },
} = methods;

useEffect(() => {
if (isEqual(resetPasswordFormDefaultValues, getValues())) return;

if (
step === ResetPasswordStep.ResetPasswordConfirmation ||
(criticalError && criticalError.errorCode === "invalid_transaction")
(criticalError &&
["invalid_transaction", "knowledgebase_mismatch"].includes(
criticalError.errorCode
))
)
return;

Expand All @@ -115,7 +127,7 @@ export const ResetPasswordPage = ({ settings }: ResetPasswordPageProps) => {
return () => {
window.removeEventListener("beforeunload", handleTabBeforeUnload);
};
}, [step, criticalError]);
}, [step, criticalError, getValues()]);

const getResetPasswordContent = (step: ResetPasswordStep) => {
switch (step) {
Expand All @@ -137,9 +149,9 @@ export const ResetPasswordPage = ({ settings }: ResetPasswordPageProps) => {
return (
<>
{criticalError &&
["invalid_transaction", "knowledgebase_mismatch"].includes(criticalError.errorCode) && (
<ResetPasswordPopover />
)}
["invalid_transaction", "knowledgebase_mismatch"].includes(
criticalError.errorCode
) && <ResetPasswordPopover />}
<Form {...methods}>
<form>{getResetPasswordContent(step)}</form>
</Form>
Expand Down
50 changes: 45 additions & 5 deletions signup-ui/src/pages/SignUpPage/SignUpPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useCallback, useMemo } from "react";
import { useCallback, useEffect, useMemo } from "react";
import { yupResolver } from "@hookform/resolvers/yup";
import { useMutationState } from "@tanstack/react-query";
import { isEqual } from "lodash";
import { Resolver, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import * as yup from "yup";

import { Button } from "~components/ui/button";
import { Form } from "~components/ui/form";
import { keys as mutationKeys } from "~pages/shared/mutations";
import {
validateCaptchaToken,
validateConfirmPassword,
Expand All @@ -14,7 +17,7 @@ import {
validatePassword,
validateUsername,
} from "~pages/shared/validation";
import { SettingsDto } from "~typings/types";
import { SettingsDto, VerifyChallengeResponseDto } from "~typings/types";

import { AccountRegistrationStatus } from "./AccountRegistrationStatus/AccountRegistrationStatus";
import AccountSetup from "./AccountSetup";
Expand Down Expand Up @@ -109,6 +112,43 @@ export const SignUpPage = ({ settings }: SignUpPageProps) => {
mode: "onBlur",
});

const { getValues } = methods;

const [challengeVerification] = useMutationState<VerifyChallengeResponseDto>({
filters: {
mutationKey: mutationKeys.challengeVerification,
status: "success",
},
select: (mutation) => mutation.state.data as VerifyChallengeResponseDto,
});

useEffect(() => {
if (isEqual(signUpFormDefaultValues, getValues())) return;

if (
(step === SignUpStep.PhoneStatus &&
challengeVerification.errors.length > 0 &&
["already-registered", "identifier_already_registered"].includes(
challengeVerification.errors[0].errorCode
)) ||
step === SignUpStep.AccountRegistrationStatus ||
(criticalError && criticalError.errorCode === "invalid_transaction")
)
return;

const handleTabBeforeUnload = (event: BeforeUnloadEvent) => {
event.preventDefault();

return (event.returnValue = t("reset_password_discontinue_prompt"));
};

window.addEventListener("beforeunload", handleTabBeforeUnload);

return () => {
window.removeEventListener("beforeunload", handleTabBeforeUnload);
};
}, [step, criticalError, getValues()]);

const getSignUpStepContent = (step: SignUpStep) => {
switch (step) {
case SignUpStep.Phone:
Expand All @@ -131,9 +171,9 @@ export const SignUpPage = ({ settings }: SignUpPageProps) => {
return (
<>
{criticalError &&
["invalid_transaction", "identifier_already_registered"].includes(criticalError.errorCode) && (
<SignUpPopover />
)}
["invalid_transaction", "identifier_already_registered"].includes(
criticalError.errorCode
) && <SignUpPopover />}
<Form {...methods}>
<form>{getSignUpStepContent(step)}</form>
</Form>
Expand Down

0 comments on commit b63bb8e

Please sign in to comment.