Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/lovely-pandas-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slashid/react": patch
---

Fix the subscriptions logic in the OTP component
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"devDependencies": {
"@faker-js/faker": "^8.0.2",
"@slashid/slashid": "3.29.0",
"@slashid/slashid": "3.29.4",
"@storybook/addon-essentials": "7.6.19",
"@storybook/addon-interactions": "7.4.0",
"@storybook/addon-links": "7.4.0",
Expand Down Expand Up @@ -95,7 +95,7 @@
"yalc": "1.0.0-pre.53"
},
"peerDependencies": {
"@slashid/slashid": ">= 3.28.0",
"@slashid/slashid": ">= 3.29.4",
"react": ">=16",
"react-dom": ">=16"
}
Expand Down
30 changes: 18 additions & 12 deletions packages/react/src/components/form/authenticating/otp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ function mapFormStateToMessageState(
export const OTPState = ({ flowState }: Props) => {
const { text } = useConfiguration();
const { sid, subscribe, unsubscribe } = useSlashID();
const { values, registerField, registerSubmit, setError, clearError } =
useForm();
const {
values,
registerField,
registerSubmit,
setError,
clearError,
resetForm,
} = useForm();
const [formState, setFormState] = useState<FormState>("initial");
const submitInputRef = useRef<HTMLInputElement>(null);

Expand All @@ -81,24 +87,24 @@ export const OTPState = ({ flowState }: Props) => {
});
}, [factor, formState, handle]);

useEffect(() => {
const onOtpCodeSent = () => setFormState("input");
const onOtpIncorrectCodeSubmitted = () => {
setError("otp", {
message: text["authenticating.otpInput.submit.error"],
});
values["otp"] = "";
setFormState("input");
};
const onOtpCodeSent = useCallback(() => setFormState("input"), []);
const onOtpIncorrectCodeSubmitted = useCallback(() => {
resetForm();
setError("otp", {
message: text["authenticating.otpInput.submit.error"],
});
setFormState("input");
}, [resetForm, setError, text]);

useEffect(() => {
subscribe("otpCodeSent", onOtpCodeSent);
subscribe("otpIncorrectCodeSubmitted", onOtpIncorrectCodeSubmitted);

return () => {
unsubscribe("otpCodeSent", onOtpCodeSent);
unsubscribe("otpIncorrectCodeSubmitted", onOtpIncorrectCodeSubmitted);
};
}, [formState, setError, sid, subscribe, text, unsubscribe, values]);
}, [onOtpCodeSent, onOtpIncorrectCodeSubmitted, subscribe, unsubscribe]);

const handleSubmit: FormEventHandler<HTMLFormElement> = useCallback(
(e) => {
Expand Down
19 changes: 17 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading