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(clerk-js): Handle two factor redirect when authenticate with web3 #5352

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/new-plants-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Handle two factor redirect when authenticate with web3 and multifactor has been enabled
42 changes: 35 additions & 7 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,7 @@ export class Clerk implements ClerkInterface {
unsafeMetadata,
strategy,
legalAccepted,
secondFactorUrl,
}: ClerkAuthenticateWithWeb3Params): Promise<void> => {
if (__BUILD_DISABLE_RHC__) {
clerkUnsupportedEnvironmentWarning('Web3');
Expand All @@ -1860,6 +1861,9 @@ export class Clerk implements ClerkInterface {
if (!this.client || !this.environment) {
return;
}

const { displayConfig } = this.environment;

const provider = strategy.replace('web3_', '').replace('_signature', '') as Web3Provider;
const identifier = await getWeb3Identifier({ provider });
const generateSignature =
Expand All @@ -1869,9 +1873,24 @@ export class Clerk implements ClerkInterface {
? generateSignatureWithCoinbaseWallet
: generateSignatureWithOKXWallet;

const navigate = (to: string) =>
const makeNavigate = (to: string) => () =>
customNavigate && typeof customNavigate === 'function' ? customNavigate(to) : this.navigate(to);

const navigateToFactorTwo = makeNavigate(
secondFactorUrl || buildURL({ base: displayConfig.signInUrl, hashPath: '/factor-two' }, { stringify: true }),
);

const navigateToContinueSignUp = makeNavigate(
signUpContinueUrl ||
buildURL(
{
base: displayConfig.signUpUrl,
hashPath: '/continue',
},
{ stringify: true },
),
);

let signInOrSignUp: SignInResource | SignUpResource;
try {
signInOrSignUp = await this.client.signIn.authenticateWithWeb3({
Expand All @@ -1894,18 +1913,27 @@ export class Clerk implements ClerkInterface {
signInOrSignUp.status === 'missing_requirements' &&
signInOrSignUp.verifications.web3Wallet.status === 'verified'
) {
await navigate(signUpContinueUrl);
await navigateToContinueSignUp();
}
} else {
throw err;
}
}

if (signInOrSignUp.createdSessionId) {
await this.setActive({
session: signInOrSignUp.createdSessionId,
redirectUrl,
});
switch (signInOrSignUp.status) {
case 'needs_second_factor':
await navigateToFactorTwo();
break;
case 'complete':
if (signInOrSignUp.createdSessionId) {
await this.setActive({
session: signInOrSignUp.createdSessionId,
redirectUrl,
});
}
break;
default:
return;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const SignInSocialButtons = React.memo((props: SocialButtonsProps) => {
redirectUrl: redirectUrlComplete,
signUpContinueUrl: ctx.isCombinedFlow ? 'create/continue' : ctx.signUpContinueUrl,
strategy,
secondFactorUrl: 'factor-two',
})
.catch(err => web3CallbackErrorHandler(err, card.setError));
}}
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ export interface ClerkAuthenticateWithWeb3Params {
unsafeMetadata?: SignUpUnsafeMetadata;
strategy: Web3Strategy;
legalAccepted?: boolean;
secondFactorUrl?: string;
}

export type JoinWaitlistParams = {
Expand Down
Loading