Skip to content

Commit c62e734

Browse files
committed
api: add backwards compat for in-app wallet user fetching
1 parent eacd784 commit c62e734

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

apps/api/src/routes/auth.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { FastifyPluginAsync } from "fastify";
77

88
import "../middleware/chain";
99
import "../middleware/swagger";
10-
import { type GetUserResult, getUser } from "thirdweb";
1110
import type {
1211
LoginBody,
1312
LoginReply,
@@ -23,7 +22,9 @@ import {
2322
} from "../schema";
2423
import type { TdkApiContext } from "../types";
2524
import { USER_PROFILE_SELECT_FIELDS, USER_SELECT_FIELDS } from "../utils/db";
25+
import { log } from "../utils/log";
2626
import {
27+
getThirdwebUser,
2728
parseThirdwebUserEmail,
2829
transformUserProfileResponseFields,
2930
} from "../utils/user";
@@ -115,24 +116,16 @@ export const authRoutes =
115116
adminAddress = result[0];
116117
} catch {
117118
// Ignore lookup if this fails, address may not be a smart account if user connected with Web3 wallet
119+
log.warn("Error fetching admin wallet for smart account:", address);
118120
}
119121

120122
if (adminAddress) {
121-
// Look up any associated user details in the embedded wallet
122-
let thirdwebUser: GetUserResult | null | undefined;
123-
try {
124-
thirdwebUser = await getUser({
125-
client,
126-
ecosystem: {
127-
id: env.THIRDWEB_ECOSYSTEM_ID,
128-
partnerId: env.THIRDWEB_ECOSYSTEM_PARTNER_ID,
129-
},
130-
walletAddress: adminAddress,
131-
});
132-
} catch {
133-
// Ignore failures from the Thirdweb SDK, this info is "nice-to-have"
134-
}
135-
123+
const thirdwebUser = await getThirdwebUser({
124+
client,
125+
ecosystemId: env.THIRDWEB_ECOSYSTEM_ID,
126+
ecosystemPartnerId: env.THIRDWEB_ECOSYSTEM_PARTNER_ID,
127+
walletAddress: adminAddress,
128+
});
136129
if (thirdwebUser) {
137130
const email = parseThirdwebUserEmail(thirdwebUser);
138131
if (email) {

apps/api/src/utils/user.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { UserProfile } from "@prisma/client";
2-
import type { GetUserResult } from "thirdweb";
2+
import {
3+
DEFAULT_TDK_ECOSYSTEM_ID,
4+
type EcosystemIdString,
5+
} from "@treasure-dev/tdk-core";
6+
import { type GetUserResult, type ThirdwebClient, getUser } from "thirdweb";
7+
8+
import { log } from "./log";
39

410
export const transformUserProfileResponseFields = (
511
profile: Partial<UserProfile>,
@@ -12,6 +18,51 @@ export const transformUserProfileResponseFields = (
1218
profile.testnetFaucetLastUsedAt?.toISOString() ?? null,
1319
});
1420

21+
export const getThirdwebUser = async ({
22+
client,
23+
ecosystemId = DEFAULT_TDK_ECOSYSTEM_ID,
24+
ecosystemPartnerId,
25+
walletAddress,
26+
}: {
27+
client: ThirdwebClient;
28+
ecosystemId?: EcosystemIdString;
29+
ecosystemPartnerId: string;
30+
walletAddress: string;
31+
}) => {
32+
try {
33+
const ecosystemWalletUser = await getUser({
34+
client,
35+
ecosystem: {
36+
id: ecosystemId,
37+
partnerId: ecosystemPartnerId,
38+
},
39+
walletAddress,
40+
});
41+
if (ecosystemWalletUser) {
42+
return ecosystemWalletUser;
43+
}
44+
} catch (err) {
45+
// Ignore failures from the Thirdweb SDK, this info is "nice-to-have"
46+
log.warn("Error fetching Thirdweb ecosystem wallets user:", err);
47+
}
48+
49+
// Fall back to querying in-app wallets (no ecosystem ID)
50+
try {
51+
const inAppWalletUser = await getUser({
52+
client,
53+
walletAddress,
54+
});
55+
if (inAppWalletUser) {
56+
return inAppWalletUser;
57+
}
58+
} catch (err) {
59+
// Ignore failures from the Thirdweb SDK, this info is "nice-to-have"
60+
log.warn("Error fetching Thirdweb in-app wallets user:", err);
61+
}
62+
63+
return undefined;
64+
};
65+
1566
export const parseThirdwebUserEmail = (user: GetUserResult) => {
1667
if (user.email) {
1768
return user.email;

0 commit comments

Comments
 (0)