Skip to content

Commit

Permalink
api: workaround for missing thirdweb user details
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Dec 11, 2024
1 parent 8f02144 commit 2455681
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"pino": "catalog:",
"pino-pretty": "catalog:",
"thirdweb": "catalog:",
"uuid": "catalog:",
"viem": "catalog:"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- DropIndex
DROP INDEX "public"."user_profile_email_key";
2 changes: 1 addition & 1 deletion apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ model UserProfile {
// Fields
// Contact
email String? @unique
email String?
// TreasureTag
tag String?
Expand Down
24 changes: 14 additions & 10 deletions apps/api/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import type { FastifyPluginAsync } from "fastify";
import { type GetUserResult, defineChain } from "thirdweb";
import { isZkSyncChain } from "thirdweb/utils";
import { v4 as uuidv4 } from "uuid";

import "../middleware/chain";
import "../middleware/swagger";
Expand Down Expand Up @@ -126,18 +127,18 @@ export const authRoutes =
});

let userId: string;
let thirdwebUser: GetUserResult;
let thirdwebUser: GetUserResult | null;
if (foundUserSmartAccount) {
const thirdwebUserDetails = await getThirdwebUser({
ecosystemWalletAddress:
foundUserSmartAccount.ecosystemWalletAddress,
});

// TODO: switch to throwing an error when Thirdweb issues are resolved
if (!thirdwebUserDetails) {
throwUnauthorizedError(
`No Thirdweb user found for ecosystem wallet ${foundUserSmartAccount.ecosystemWalletAddress}`,
console.warn(
`No user details found for ecosystem wallet ${foundUserSmartAccount.ecosystemWalletAddress}`,
);
return;
}

userId = foundUserSmartAccount.userId;
Expand Down Expand Up @@ -168,26 +169,29 @@ export const authRoutes =
ecosystemWalletAddress: adminWalletAddress,
});

// TODO: switch to throwing an error when Thirdweb issues are resolved
if (!thirdwebUserDetails) {
throwUnauthorizedError(
`No Thirdweb user found for admin wallet ${adminWalletAddress}`,
console.warn(
`No user details found for admin wallet ${adminWalletAddress}`,
);
return;
}

const externalUserId =
thirdwebUserDetails?.userId ?? `temp-${uuidv4()}`;

const newUserSmartAccount = await db.userSmartAccount.create({
data: {
chainId,
address,
ecosystemWalletAddress: adminWalletAddress,
// If Thirdweb user ID is present, look for existing user to connect. Otherwise create new user
// Look for existing user to connect, otherwise create a new user
user: {
connectOrCreate: {
where: {
externalUserId: thirdwebUserDetails.userId,
externalUserId,
},
create: {
externalUserId: thirdwebUserDetails.userId,
externalUserId,
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2455681

Please sign in to comment.