Skip to content

Commit c4836f9

Browse files
authored
fix(clerk-js): Handle two factor redirect when authenticate with web3 (#5352)
1 parent f6f275d commit c4836f9

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

.changeset/new-plants-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Handle two factor redirect when authenticate with web3 and multifactor has been enabled

packages/clerk-js/src/core/clerk.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@ export class Clerk implements ClerkInterface {
18511851
unsafeMetadata,
18521852
strategy,
18531853
legalAccepted,
1854+
secondFactorUrl,
18541855
}: ClerkAuthenticateWithWeb3Params): Promise<void> => {
18551856
if (__BUILD_DISABLE_RHC__) {
18561857
clerkUnsupportedEnvironmentWarning('Web3');
@@ -1860,6 +1861,9 @@ export class Clerk implements ClerkInterface {
18601861
if (!this.client || !this.environment) {
18611862
return;
18621863
}
1864+
1865+
const { displayConfig } = this.environment;
1866+
18631867
const provider = strategy.replace('web3_', '').replace('_signature', '') as Web3Provider;
18641868
const identifier = await getWeb3Identifier({ provider });
18651869
const generateSignature =
@@ -1869,9 +1873,24 @@ export class Clerk implements ClerkInterface {
18691873
? generateSignatureWithCoinbaseWallet
18701874
: generateSignatureWithOKXWallet;
18711875

1872-
const navigate = (to: string) =>
1876+
const makeNavigate = (to: string) => () =>
18731877
customNavigate && typeof customNavigate === 'function' ? customNavigate(to) : this.navigate(to);
18741878

1879+
const navigateToFactorTwo = makeNavigate(
1880+
secondFactorUrl || buildURL({ base: displayConfig.signInUrl, hashPath: '/factor-two' }, { stringify: true }),
1881+
);
1882+
1883+
const navigateToContinueSignUp = makeNavigate(
1884+
signUpContinueUrl ||
1885+
buildURL(
1886+
{
1887+
base: displayConfig.signUpUrl,
1888+
hashPath: '/continue',
1889+
},
1890+
{ stringify: true },
1891+
),
1892+
);
1893+
18751894
let signInOrSignUp: SignInResource | SignUpResource;
18761895
try {
18771896
signInOrSignUp = await this.client.signIn.authenticateWithWeb3({
@@ -1894,18 +1913,27 @@ export class Clerk implements ClerkInterface {
18941913
signInOrSignUp.status === 'missing_requirements' &&
18951914
signInOrSignUp.verifications.web3Wallet.status === 'verified'
18961915
) {
1897-
await navigate(signUpContinueUrl);
1916+
await navigateToContinueSignUp();
18981917
}
18991918
} else {
19001919
throw err;
19011920
}
19021921
}
19031922

1904-
if (signInOrSignUp.createdSessionId) {
1905-
await this.setActive({
1906-
session: signInOrSignUp.createdSessionId,
1907-
redirectUrl,
1908-
});
1923+
switch (signInOrSignUp.status) {
1924+
case 'needs_second_factor':
1925+
await navigateToFactorTwo();
1926+
break;
1927+
case 'complete':
1928+
if (signInOrSignUp.createdSessionId) {
1929+
await this.setActive({
1930+
session: signInOrSignUp.createdSessionId,
1931+
redirectUrl,
1932+
});
1933+
}
1934+
break;
1935+
default:
1936+
return;
19091937
}
19101938
};
19111939

packages/clerk-js/src/ui/components/SignIn/SignInSocialButtons.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const SignInSocialButtons = React.memo((props: SocialButtonsProps) => {
5454
redirectUrl: redirectUrlComplete,
5555
signUpContinueUrl: ctx.isCombinedFlow ? 'create/continue' : ctx.signUpContinueUrl,
5656
strategy,
57+
secondFactorUrl: 'factor-two',
5758
})
5859
.catch(err => web3CallbackErrorHandler(err, card.setError));
5960
}}

packages/types/src/clerk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,7 @@ export interface ClerkAuthenticateWithWeb3Params {
16121612
unsafeMetadata?: SignUpUnsafeMetadata;
16131613
strategy: Web3Strategy;
16141614
legalAccepted?: boolean;
1615+
secondFactorUrl?: string;
16151616
}
16161617

16171618
export type JoinWaitlistParams = {

0 commit comments

Comments
 (0)