Skip to content

Commit

Permalink
fix: solve the race condition on new user signup
Browse files Browse the repository at this point in the history
  • Loading branch information
moldy530 committed Nov 14, 2024
1 parent 995a210 commit aa12a59
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
8 changes: 2 additions & 6 deletions account-kit/react/src/components/auth/card/loading/oauth.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { OauthCancelledError } from "@account-kit/signer";
import { useEffect } from "react";
import { useSignerStatus } from "../../../../hooks/useSignerStatus.js";
import { ContinueWithOAuth } from "../../../../icons/oauth.js";
import { capitalize } from "../../../../utils.js";
import { useAuthContext } from "../../context.js";
import { useOAuthVerify } from "../../hooks/useOAuthVerify.js";
import { ConnectionError } from "../error/connection-error.js";
import { OauthCancelledError } from "@account-kit/signer";

export const CompletingOAuth = () => {
const { isConnected } = useSignerStatus();
Expand All @@ -15,11 +15,7 @@ export const CompletingOAuth = () => {

useEffect(() => {
if (isConnected) {
if (authStep.createPasskeyAfter) {
setAuthStep({ type: "passkey_create" });
} else {
setAuthStep({ type: "complete" });
}
setAuthStep({ type: "complete" });
} else if (oauthWasCancelled) {
setAuthStep({ type: "initial" });
}
Expand Down
12 changes: 6 additions & 6 deletions account-kit/react/src/components/auth/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback } from "react";
import { useNewUserSignup } from "../../hooks/internal/useNewUserSignup.js";
import { useAuthModal } from "../../hooks/useAuthModal.js";
import { useSignerStatus } from "../../hooks/useSignerStatus.js";
import { useUiConfig } from "../../hooks/useUiConfig.js";
import { useSignerStatus } from "../../index.js";
import { Dialog } from "../dialog/dialog.js";
import { AuthCardContent } from "./card/index.js";
import { useAuthContext } from "./context.js";
Expand All @@ -16,20 +16,20 @@ export const AuthModal = () => {
})
);

const { setAuthStep } = useAuthContext();
const { setAuthStep, authStep } = useAuthContext();
const { isOpen, closeAuthModal, openAuthModal } = useAuthModal();

const handleSignup = useCallback(() => {
if (addPasskeyOnSignup && !isOpen) {
console.log("handling sign up");
if (addPasskeyOnSignup) {
openAuthModal();

setAuthStep({
type: "passkey_create",
});
}
}, [addPasskeyOnSignup, isOpen, openAuthModal, setAuthStep]);
}, [addPasskeyOnSignup, openAuthModal, setAuthStep]);

useNewUserSignup(handleSignup, isConnected);
useNewUserSignup(handleSignup, isConnected && authStep.type === "complete");

return (
<Dialog isOpen={isOpen} onClose={closeAuthModal}>
Expand Down
2 changes: 1 addition & 1 deletion account-kit/react/src/hooks/useAuthModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const useAuthModal = () => {

const openAuthModal = useCallback(() => setModalOpen(true), [setModalOpen]);
const closeAuthModal = useCallback(() => {
console.log("closing modal");
console.trace("closing modal");
setModalOpen(false);
}, [setModalOpen]);

Expand Down
1 change: 0 additions & 1 deletion account-kit/signer/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,6 @@ export abstract class BaseAlchemySigner<TClient extends BaseSignerClient>

// trigger new user event on signer from client
this.inner.on("newUserSignupClient", () => {
console.log("event triggered by client");
this.emitNewUserEvent(true);
});

Expand Down
17 changes: 10 additions & 7 deletions account-kit/signer/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { BaseError, ConnectionConfigSchema } from "@aa-sdk/core";
import { getWebAuthnAttestation } from "@turnkey/http";
import { IframeStamper } from "@turnkey/iframe-stamper";
import { WebauthnStamper } from "@turnkey/webauthn-stamper";
import { z } from "zod";
import { OAuthProvidersError } from "../errors.js";
import { getDefaultScopeAndClaims, getOauthNonce } from "../oauth.js";
import type { AuthParams, OauthMode } from "../signer.js";
import { base64UrlEncode } from "../utils/base64UrlEncode.js";
Expand All @@ -18,8 +20,6 @@ import type {
OauthParams,
User,
} from "./types.js";
import { OAuthProvidersError } from "../errors.js";
import { z } from "zod";

const CHECK_CLOSE_INTERVAL = 500;

Expand Down Expand Up @@ -462,6 +462,7 @@ export class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams>
"_blank",
"popup,width=500,height=600"
);
const eventEmitter = this.eventEmitter;
return new Promise((resolve, reject) => {
const handleMessage = (event: MessageEvent) => {
if (!event.data) {
Expand All @@ -475,10 +476,6 @@ export class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams>
alchemyError,
} = event.data;
if (bundle && orgId && idToken) {
if (isSignup || true) {
this.eventEmitter.emit("newUserSignupClient");
console.log("we emitted an event from the client!");
}
cleanup();
popup?.close();
this.completeAuthWithBundle({
Expand All @@ -487,7 +484,13 @@ export class AlchemySignerWebClient extends BaseSignerClient<ExportWalletParams>
connectedEventName: "connectedOauth",
idToken,
authenticatingType: "oauth",
}).then(resolve, reject);
}).then((user) => {
if (isSignup) {
eventEmitter.emit("newUserSignupClient");
}

resolve(user);
}, reject);
} else if (alchemyError) {
cleanup();
popup?.close();
Expand Down
2 changes: 1 addition & 1 deletion examples/embedded-accounts-quickstart

0 comments on commit aa12a59

Please sign in to comment.