From f881a1bbeec845d7905d1124d01b13a20772abf0 Mon Sep 17 00:00:00 2001 From: Eunju Huss Date: Wed, 18 Dec 2024 13:01:45 +0100 Subject: [PATCH] WIP, captcha for reset pw --- .../ResetPassword/ResetPasswordApp.tsx | 1 + .../ResetPassword/ResetPasswordCaptcha.tsx | 73 +++++++++++++++++++ src/machines/ResetPasswordMachine.ts | 13 ++++ src/machines/ResetPasswordMachine.typegen.ts | 2 + 4 files changed, 89 insertions(+) create mode 100644 src/components/ResetPassword/ResetPasswordCaptcha.tsx diff --git a/src/components/ResetPassword/ResetPasswordApp.tsx b/src/components/ResetPassword/ResetPasswordApp.tsx index 17675e095..1280357bd 100644 --- a/src/components/ResetPassword/ResetPasswordApp.tsx +++ b/src/components/ResetPassword/ResetPasswordApp.tsx @@ -58,6 +58,7 @@ export function ResetPasswordApp(): JSX.Element { {state.matches("AskForEmailOrConfirmEmail") && } {state.matches("AskForEmailOrConfirmEmail.ResetPasswordConfirmEmail") && } {state.matches("AskForEmailOrConfirmEmail.ResetPasswordEnterEmail") && } + {state.matches("AskForEmailOrConfirmEmail.ResetPasswordCaptcha") && } {state.matches("AskForEmailOrConfirmEmail.EmailLinkSent") && } {state.matches("HandleExtraSecurities.HandleExtraSecurities") && } {state.matches("HandleExtraSecurities.ResetPasswordPhoneVerification") && } diff --git a/src/components/ResetPassword/ResetPasswordCaptcha.tsx b/src/components/ResetPassword/ResetPasswordCaptcha.tsx new file mode 100644 index 000000000..ddc4fcd1a --- /dev/null +++ b/src/components/ResetPassword/ResetPasswordCaptcha.tsx @@ -0,0 +1,73 @@ +import { getCaptchaRequest } from "apis/eduidPhone"; +import { GetCaptchaResponse } from "apis/eduidSignup"; +import { InternalCaptcha } from "components/Common/Captcha"; +import { useAppDispatch, useAppSelector } from "eduid-hooks"; +import { Fragment, useContext, useEffect } from "react"; +import { FormattedMessage } from "react-intl"; +import { ResetPasswordGlobalStateContext } from "./ResetPasswordGlobalState"; + +export interface CaptchaProps { + readonly handleCaptchaCancel: () => void; + readonly handleCaptchaCompleted: (response: string) => void; + getCaptcha: () => Promise; +} + +export function ResetPasswordCaptcha(): JSX.Element | null { + const state = useAppSelector((state) => state.resetPassword); + const resetPasswordContext = useContext(ResetPasswordGlobalStateContext); + const dispatch = useAppDispatch(); + + useEffect(() => { + // if (state?.captcha.completed) { + // resetPasswordContext.resetPasswordService.send({ type: "API_SUCCESS" }); + // } + }, [state]); + + async function getCaptcha() { + // temporary code + const res = await dispatch(getCaptchaRequest()); + if (getCaptchaRequest.fulfilled.match(res)) { + return res.payload; + } + } + + function handleCaptchaCancel() { + // resetPasswordContext.resetPasswordService.send({ type: "ABORT" }); + } + + function handleCaptchaCompleted(response: string) { + if (response) { + // dispatch(signupSlice.actions.setCaptchaResponse({ internal_response: response })); + // signupContext.signupService.send({ type: "COMPLETE" }); + } + } + + const args = { handleCaptchaCancel, handleCaptchaCompleted }; + + // If the user has already completed the captcha, don't show it again + // if (state?.captcha.completed) { + // return null; + // } + + return ( + +

+ +

+ +
+

+ +

+
+ + +
+ ); +} diff --git a/src/machines/ResetPasswordMachine.ts b/src/machines/ResetPasswordMachine.ts index 5dbd30715..67541efbf 100644 --- a/src/machines/ResetPasswordMachine.ts +++ b/src/machines/ResetPasswordMachine.ts @@ -75,6 +75,19 @@ export function createResetPasswordMachine() { }, }, ResetPasswordEnterEmail: { + on: { + API_SUCCESS: { + target: "ResetPasswordCaptcha", + }, + API_FAIL: { + target: "ResetPasswordEnterEmail", + }, + GO_BACK: { + target: "#resetPassword.ReturnToPrevious", + }, + }, + }, + ResetPasswordCaptcha: { on: { API_SUCCESS: { target: "EmailLinkSent", diff --git a/src/machines/ResetPasswordMachine.typegen.ts b/src/machines/ResetPasswordMachine.typegen.ts index 8676acce0..efbcc50e5 100644 --- a/src/machines/ResetPasswordMachine.typegen.ts +++ b/src/machines/ResetPasswordMachine.typegen.ts @@ -21,6 +21,7 @@ export interface Typegen0 { | "AskForEmailOrConfirmEmail.AskForEmailOrConfirmEmail" | "AskForEmailOrConfirmEmail.EmailLinkSent" | "AskForEmailOrConfirmEmail.Finished" + | "AskForEmailOrConfirmEmail.ResetPasswordCaptcha" | "AskForEmailOrConfirmEmail.ResetPasswordConfirmEmail" | "AskForEmailOrConfirmEmail.ResetPasswordEnterEmail" | "FinaliseResetPassword" @@ -41,6 +42,7 @@ export interface Typegen0 { | "AskForEmailOrConfirmEmail" | "EmailLinkSent" | "Finished" + | "ResetPasswordCaptcha" | "ResetPasswordConfirmEmail" | "ResetPasswordEnterEmail"; "FinaliseResetPassword"?: "ResetPasswordSuccess" | "SetNewPassword";