Skip to content

Commit fd30702

Browse files
committed
fix(thirdweb): mobile auth (#3886)
### TL;DR Improved social authentication flexibility by adding support for an optional `redirectUrl` parameter and a new `mobile` mode. ### What changed? - `getLoginPath.ts`: Added handling for `redirectUrl` and a new mode `mobile`. - `native-auth.ts`: Updated social login to use `mobile` mode and include `redirectUrl`. - `auth/middleware.ts`: Enhanced error handling for recovery codes by attempting fallback recovery code method. - `native-connector.ts`: Fixed typo in console error message and ensured `redirectUrl` defaults correctly for native social logins. ### How to test? 1. Initiate a social login with the added `mobile` mode and verify if the `redirectUrl` functions correctly. 2. Trigger an error scenario in recovery code fetching and check that the fallback method is attempted and appropriate error is thrown if both fail. 3. Check the console for corrected error messages. ### Why make this change? To increase the robustness and flexibility of social authentication by supporting mobile specific workflows and enhanced error resilience. --- ## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview The focus of this PR is to enhance mobile authentication in the thirdweb application by updating redirect URLs and recovery code handling. ### Detailed summary - Updated authentication mode to "mobile" instead of "popup" - Added `redirectUrl` parameter for mobile authentication - Improved error handling for recovery code retrieval - Modified login path generation to support mobile authentication > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 6cd279b commit fd30702

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

packages/thirdweb/src/wallets/in-app/core/authentication/getLoginPath.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export const getSocialAuthLoginPath = ({
88
client,
99
ecosystem,
1010
mode = "popup",
11+
redirectUrl,
1112
}: {
1213
authOption: SocialAuthOption;
1314
client: ThirdwebClient;
1415
ecosystem?: Ecosystem;
15-
mode?: "popup" | "redirect";
16+
mode?: "popup" | "redirect" | "mobile";
17+
redirectUrl?: string;
1618
}) => {
1719
let baseUrl = `${getThirdwebBaseUrl("inAppWallet")}/api/2024-05-05/login/${authOption}?clientId=${client.clientId}`;
1820
if (ecosystem?.partnerId) {
@@ -22,10 +24,17 @@ export const getSocialAuthLoginPath = ({
2224
}
2325

2426
if (mode === "redirect") {
25-
const redirectUrl = new URL(window.location.href);
26-
redirectUrl.searchParams.set("walletId", ecosystem?.id || "inApp");
27-
redirectUrl.searchParams.set("authProvider", authOption);
28-
baseUrl = `${baseUrl}&redirectUrl=${encodeURIComponent(redirectUrl.toString())}`;
27+
const formattedRedirectUrl = new URL(redirectUrl || window.location.href);
28+
formattedRedirectUrl.searchParams.set("walletId", ecosystem?.id || "inApp");
29+
formattedRedirectUrl.searchParams.set("authProvider", authOption);
30+
baseUrl = `${baseUrl}&redirectUrl=${encodeURIComponent(formattedRedirectUrl.toString())}`;
31+
}
32+
33+
if (mode === "mobile") {
34+
if (!redirectUrl) {
35+
throw new Error("Redirect URL is required for mobile authentication");
36+
}
37+
baseUrl = `${baseUrl}&redirectUrl=${encodeURIComponent(redirectUrl)}`;
2938
}
3039

3140
return baseUrl;

packages/thirdweb/src/wallets/in-app/native/auth/native-auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ export async function socialLogin(
211211
const loginUrl = getSocialAuthLoginPath({
212212
authOption: auth.strategy,
213213
client,
214-
mode: "popup",
214+
mode: "mobile",
215+
redirectUrl: auth.redirectUrl,
215216
});
216217

217218
const result = await WebBrowser.openAuthSessionAsync(

packages/thirdweb/src/wallets/in-app/native/helpers/auth/middleware.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
setWallerUserDetails,
1212
} from "../storage/local.js";
1313
import { setUpNewUserWallet } from "../wallet/creation.js";
14-
import { getCognitoRecoveryPasswordV2 } from "../wallet/recoveryCode.js";
14+
import {
15+
getCognitoRecoveryPasswordV1,
16+
getCognitoRecoveryPasswordV2,
17+
} from "../wallet/recoveryCode.js";
1518
import { setUpShareForNewDevice } from "../wallet/retrieval.js";
1619

1720
export async function preAuth(args: {
@@ -153,7 +156,9 @@ async function getRecoveryCode(
153156
try {
154157
return await getCognitoRecoveryPasswordV2(client);
155158
} catch (e) {
156-
throw new Error("Something went wrong getting cognito recovery code");
159+
return await getCognitoRecoveryPasswordV1(client).catch(() => {
160+
throw new Error("Something went wrong getting cognito recovery code");
161+
});
157162
}
158163
}
159164
} else if (

packages/thirdweb/src/wallets/in-app/native/native-connector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class InAppNativeConnector implements InAppConnector {
113113
case "apple": {
114114
const ExpoLinking = require("expo-linking");
115115
const redirectUrl =
116-
params.redirectUrl || (ExpoLinking.createURL("") as string);
116+
params.redirectUrl || (ExpoLinking.createURL("") as string); // Will default to the app scheme
117117
return this.socialLogin({
118118
strategy,
119119
redirectUrl,
@@ -167,7 +167,7 @@ export class InAppNativeConnector implements InAppConnector {
167167
},
168168
};
169169
} catch (error) {
170-
console.error(`Error while validating otp: ${error}`);
170+
console.error(`Error while validating OTP: ${error}`);
171171
if (error instanceof Error) {
172172
throw new Error(`Error while validating otp: ${error.message}`);
173173
}

0 commit comments

Comments
 (0)