Skip to content

Commit

Permalink
fix(reset) validate email on reset password, avoid submitting with an…
Browse files Browse the repository at this point in the history
… invalid email address. fixes #385

Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Dec 18, 2024
1 parent e2aaea7 commit 52eb068
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions ui/components/NodeInputEmail.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getNodeLabel } from "@ory/integrations/ui";
import { Input } from "@canonical/react-components";
import React, { FC } from "react";
import React, { FC, useEffect } from "react";
import { NodeInputProps } from "./helpers";

export const NodeInputEmail: FC<NodeInputProps> = ({
Expand All @@ -13,7 +13,7 @@ export const NodeInputEmail: FC<NodeInputProps> = ({
}) => {
const [hasLocalValidation, setLocalValidation] = React.useState(false);

const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const emailRegex = /^[^\s@]+@[^\s@]+$/;
const isInvalid = !emailRegex.test((value as string) ?? "") && value !== "";
const localError = isInvalid ? "Incorrect email address" : undefined;
const message = node.messages.map(({ text }) => text).join(" ");
Expand All @@ -23,10 +23,28 @@ export const NodeInputEmail: FC<NodeInputProps> = ({

const error = hasLocalValidation ? localError : upstreamError;

useEffect(() => {
const submitBtn =
document.getElementsByClassName("p-button--positive")?.[0];
if (error) {
submitBtn?.setAttribute("disabled", "");
} else {
submitBtn?.removeAttribute("disabled");
}
}, [error]);

if (value == undefined) {
void setValue(defaultValue);
}

const submitOnEnter = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter" && !error) {
e.preventDefault();
e.stopPropagation();
void dispatchSubmit(e, "code");
}
};

return (
<Input
type="email"
Expand All @@ -37,13 +55,7 @@ export const NodeInputEmail: FC<NodeInputProps> = ({
error={error}
onChange={(e) => void setValue(e.target.value)}
onBlur={() => setLocalValidation(true)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
e.stopPropagation();
void dispatchSubmit(e, "code");
}
}}
onKeyDown={submitOnEnter}
/>
);
};

0 comments on commit 52eb068

Please sign in to comment.