Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keeping otp input in view while the code is validated #1234

Closed
wants to merge 2 commits into from
Closed
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
1 change: 0 additions & 1 deletion account-kit/react/src/components/auth/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export const AuthCardContent = ({
case "otp_verify":
case "passkey_create":
case "oauth_completing":
case "otp_completing":
disconnect(config); // Terminate any inflight authentication
didGoBack.current = true;
setAuthStep({ type: "initial" });
Expand Down
78 changes: 28 additions & 50 deletions account-kit/react/src/components/auth/card/loading/otp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useState } from "react";
import { EmailIllustration } from "../../../../icons/illustrations/email.js";
import { ls } from "../../../../strings.js";
import {
Expand All @@ -13,25 +13,40 @@ import { useAuthenticate } from "../../../../hooks/useAuthenticate.js";
import { useSignerStatus } from "../../../../hooks/useSignerStatus.js";

export const LoadingOtp = () => {
const { isConnected } = useSignerStatus();
const { authStep } = useAuthContext("otp_verify");
const [otpCode, setOtpCode] = useState<OTPCodeType>(initialOTPValue);
const [errorText, setErrorText] = useState(authStep.error?.message || "");
const [isDisabled, setIsDisabled] = useState(false);
const { setAuthStep } = useAuthContext();
const resetOTP = () => {
const resetOTP = (errorText = "") => {
setOtpCode(initialOTPValue);
setErrorText("");
setErrorText(errorText);
setIsDisabled(false);
};

useEffect(() => {
const { authenticate } = useAuthenticate({
onError: (error: any) => {
console.error(error);
const { email } = authStep;
setAuthStep({ type: "otp_verify", email, error });
resetOTP(error.message);
},
onSuccess: () => {
if (isConnected) {
setAuthStep({ type: "complete" });
}
},
});

const setValue = (otpCode: OTPCodeType) => {
setOtpCode(otpCode);
if (isOTPCodeType(otpCode)) {
setAuthStep({
type: "otp_completing",
email: authStep.email,
otp: otpCode.join(""),
});
setIsDisabled(true);
const otp = otpCode.join("");
authenticate({ type: "otp", otpCode: otp });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [otpCode]);
};

return (
<div className="flex flex-col items-center">
Expand All @@ -53,50 +68,13 @@ export const LoadingOtp = () => {
{authStep.email}
</p>
<OTPInput
disabled={isDisabled}
value={otpCode}
setValue={setOtpCode}
setValue={setValue}
setErrorText={setErrorText}
errorText={errorText}
handleReset={resetOTP}
/>
</div>
);
};

export const CompletingOtp = () => {
const { isConnected } = useSignerStatus();
const { setAuthStep, authStep } = useAuthContext("otp_completing");
const { authenticate } = useAuthenticate({
onError: (error: any) => {
console.error(error);
const { email } = authStep;
setAuthStep({ type: "otp_verify", email, error });
},
onSuccess: () => {
if (isConnected && authStep.createPasskeyAfter) {
setAuthStep({ type: "passkey_create" });
} else if (isConnected) {
setAuthStep({ type: "complete" });
}
},
});

useEffect(() => {
authenticate({ type: "otp", otpCode: authStep.otp });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<div className="flex flex-col items-center justify-center ">
<div className="flex flex-col items-center justify-center h-12 w-12 mb-5">
<Spinner />
</div>
<h2 className="text-fg-primary font-semibold text-lg mb-2">
{ls.completingOtp.title}
</h2>
<p className="text-fg-secondary text-center text-sm">
{ls.completingOtp.body}
</p>
</div>
);
};
4 changes: 1 addition & 3 deletions account-kit/react/src/components/auth/card/steps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CompletingOAuth } from "./loading/oauth.js";
import { LoadingPasskeyAuth } from "./loading/passkey.js";
import { MainAuthContent } from "./main.js";
import { PasskeyAdded } from "./passkey-added.js";
import { CompletingOtp, LoadingOtp } from "./loading/otp.js";
import { LoadingOtp } from "./loading/otp.js";

export const Step = () => {
const { authStep } = useAuthContext();
Expand All @@ -21,8 +21,6 @@ export const Step = () => {
return <CompletingEmailAuth />;
case "oauth_completing":
return <CompletingOAuth />;
case "otp_completing":
return <CompletingOtp />;
case "passkey_create":
return <AddPasskey />;
case "passkey_create_success":
Expand Down
7 changes: 0 additions & 7 deletions account-kit/react/src/components/auth/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ export type AuthStep =
createPasskeyAfter?: boolean;
error?: Error;
}
| {
type: "otp_completing";
email: string;
createPasskeyAfter?: boolean;
otp: string;
error?: Error;
}
| { type: "initial"; error?: Error }
| { type: "complete" }
| { type: "eoa_connect"; connector: Connector; error?: Error }
Expand Down
1 change: 0 additions & 1 deletion account-kit/react/src/components/auth/sections/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const RenderFooterText = ({ authStep }: FooterProps) => {
case "oauth_completing":
return <OAuthContactSupport />;
case "email_completing":
case "otp_completing":
case "passkey_create_success":
case "eoa_connect":
case "pick_eoa":
Expand Down
2 changes: 1 addition & 1 deletion account-kit/react/src/components/otp-input/otp-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const OTP_LENGTH = 6;
type OTPInputProps = {
errorText?: string;
value: OTPCodeType;
setValue: React.Dispatch<React.SetStateAction<OTPCodeType>>;
setValue: (otpCode: OTPCodeType) => void;
setErrorText: React.Dispatch<React.SetStateAction<string>>;
disabled?: boolean;
handleReset: () => void;
Expand Down
Loading