Skip to content

react-wallet-kit: add onOtpVerified callback - #1644

Open
sergeytimoshin wants to merge 1 commit into
tkhq:mainfrom
sergeytimoshin:react-wallet-kit-on-otp-verified
Open

sergeytimoshin wants to merge 1 commit into
tkhq:mainfrom
sergeytimoshin:react-wallet-kit-on-otp-verified

Conversation

@sergeytimoshin

Copy link
Copy Markdown

Summary & Motivation

Adds an optional callbacks.onOtpVerified to @turnkey/react-wallet-kit. When it is set, the auth modal hands a verified email or SMS code over to the host instead of completing the login in the SDK (account lookup, then loginWithOtp or signUpWithOtp). We build on Turnkey and finish OTP sign-ins through our own backend, and the SDK had no hook for that: onOauthRedirect already hands an OAuth ID token to the host and skips the SDK's completion, so this is the same thing for OTP.

Signature:

onOtpVerified?: (params: {
  verificationToken: string;
  publicKey: string;
  contact: string;
  otpType: OtpType;
  sessionKey?: string;
}) => Promise<void>;

Semantics: the SDK generates the API key pair and verifies the code with the auth proxy (verifyOtp), then awaits the callback with the verification token bound to the key pair's public key. The host finishes the login and stores the session it obtains with storeSession. When the promise resolves the modal closes, as after a successful login. When it rejects, the modal shows the error like any other OTP error and the generated key pair is deleted. Without the callback nothing changes.

Like onOauthRedirect (consulted in completeOAuthFlow, not in completeOauth), the callback is consulted in the modal's OtpVerification page only; calling completeOtp directly still completes the login in the SDK, and its return type is unchanged. callbacks is exposed on the client context as an @internal field, next to config, so the page can read it.

How I Tested These Changes

Added src/tests/otp-test.tsx, which renders OtpVerification with mocked hooks and checks the three paths: no callback (SDK completeOtp, modal closes), callback resolves (verifyOtp then the callback with the expected params, completeOtp not called, modal closes), callback rejects (error text shown, deleteApiKeyPair called, modal stays open), plus a failed verification never reaching the host. pnpm test, pnpm typecheck, pnpm build and prettier --check pass for the package, and the change is live in our app against a pnpm patch of the same logic.

Did you add a changeset?

Yes, a minor bump for @turnkey/react-wallet-kit.

Lets a host finish an email or SMS OTP login itself once the auth proxy
has verified the code, instead of the SDK's account lookup and
loginWithOtp/signUpWithOtp. The auth modal verifies the code, hands the
verification token and the generated key pair's public key to
callbacks.onOtpVerified, closes when it resolves, and shows the error
and deletes the key pair when it rejects. Mirrors onOauthRedirect.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant