Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/components/Main/Authentication/Registration/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyboardEvent, useEffect } from "react";
import { KeyboardEvent, useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { sendUserActionTrackingEvent } from "../../../../utils/actions/sendUserActionTrackingEvent";
import { isValidPasswordFormat } from "../../../../utils/isPasswordFormatValid";
Expand Down Expand Up @@ -32,7 +32,7 @@ const validatePassword = (password: string): string | boolean => {
}

if (!isValidPasswordFormat(password)) {
return "Password must contain one special character";
return "Password must contain one special character !@#$%^&*-";
}

return true;
Expand All @@ -52,7 +52,8 @@ export const Registration = () => {
watch,
setError,
clearErrors,
formState: { errors, isValid },
trigger,
formState: { errors, isValid, touchedFields },
setFocus
} = useForm<RegisterFormValues>({
mode: "onChange",
Expand All @@ -66,12 +67,11 @@ export const Registration = () => {
errors: resultErrors
} = useRegistration();

const [responseStatus, setResponseStatus] = useState<string>();

useEffect(() => {
if (resultErrors?.length > 0) {
setError("root", {
type: "validate",
message: resultErrors.map((x) => x.description).join("\r\n")
});
setResponseStatus(resultErrors.map((x) => x.description).join("\n"));
}
}, [setError, resultErrors]);

Expand All @@ -81,7 +81,7 @@ export const Registration = () => {

useEffect(() => {
watch(() => {
clearErrors();
setResponseStatus(undefined);
});
}, [clearErrors, watch]);

Expand All @@ -95,6 +95,7 @@ export const Registration = () => {
onSubmit(values);
}
};

const errorMessage =
Object.values(errors).length > 0 ? Object.values(errors)[0].message : "";

Expand Down Expand Up @@ -127,7 +128,13 @@ export const Registration = () => {
defaultValue={""}
rules={{
required: "Password is required",
validate: validatePassword
validate: (pass) => {
if (touchedFields.confirmPassword) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
trigger("confirmPassword");
}
return validatePassword(pass);
}
}}
render={({ field }) => (
<TextField
Expand Down Expand Up @@ -169,6 +176,7 @@ export const Registration = () => {
/>
</s.Form>
{errorMessage && <s.ErrorMessage>{errorMessage}</s.ErrorMessage>}
{responseStatus && <s.ErrorMessage>{responseStatus}</s.ErrorMessage>}
{isSucceed && (
<s.SuccessMessage>User created successfully!</s.SuccessMessage>
)}
Expand Down
1 change: 1 addition & 0 deletions src/components/Main/Authentication/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const InputForm = styled.div`
display: flex;
flex-direction: column;
gap: 12px;
max-width: 250px;
`;

export const InfoMessage = styled.div`
Expand Down
3 changes: 2 additions & 1 deletion src/utils/isPasswordFormatValid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// password should have at least 6 symbols and could be alphanumeric with at least one spec symbol
const PASSWORD_REGEX =
/^(?=.*[A-Za-z0-9])(?=.*[!@#$%^&*])[A-Za-z0-9!@#$%^&*]{6,}$/gm;
/^(?=.*[A-Za-z0-9])(?=.*[!@#$%^&*-])[A-Za-z0-9!@#$%^&*-]{6,}$/gm;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the description for this regexp


export const isValidPasswordFormat = (password: string): boolean => {
return new RegExp(PASSWORD_REGEX).test(password);
Expand Down