Skip to content

Commit f336ee7

Browse files
use shouldUpdateSessionKey in login flow
1 parent 84d1623 commit f336ee7

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

apps/login/src/components/login-form.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import { toast } from "sonner";
66
import { getContract, sendAndConfirmTransaction } from "thirdweb";
77
import type { Chain, ThirdwebClient } from "thirdweb";
88
import { defineChain } from "thirdweb/chains";
9-
import { addSessionKey, isActiveSigner } from "thirdweb/extensions/erc4337";
9+
import {
10+
addSessionKey,
11+
shouldUpdateSessionKey,
12+
} from "thirdweb/extensions/erc4337";
1013
import {
1114
ConnectButton,
1215
ConnectEmbed,
1316
useActiveAccount,
1417
useActiveWallet,
1518
useSiweAuth,
1619
} from "thirdweb/react";
17-
import { isContractDeployed } from "thirdweb/utils";
1820
import { type Account, inAppWallet } from "thirdweb/wallets";
1921
import { createCode } from "../actions/create-code";
2022
import {
@@ -163,39 +165,37 @@ async function ensureSessionKey(options: {
163165
client: options.client,
164166
});
165167

168+
const newPermissions = {
169+
approvedTargets:
170+
typeof options.permissions["contracts:write"] === "boolean"
171+
? ("*" as const)
172+
: options.permissions["contracts:write"],
173+
permissionEndTimestamp: options.permissions.expiration,
174+
nativeTokenLimitPerTransaction: options.permissions["native:spend"],
175+
};
176+
177+
const needsUpdate = await shouldUpdateSessionKey({
178+
accountContract,
179+
sessionKeyAddress: options.sessionKeySignerAddress,
180+
newPermissions,
181+
});
182+
166183
// check if already added
167-
const accountDeployed = await isContractDeployed(accountContract);
168-
if (accountDeployed) {
169-
if (
170-
await isActiveSigner({
171-
contract: accountContract,
172-
signer: options.sessionKeySignerAddress,
173-
})
174-
) {
175-
return {
176-
success: true,
177-
message: "Session key already added",
178-
transaction: null,
179-
};
180-
}
184+
if (!needsUpdate) {
185+
return {
186+
success: true,
187+
message: "Session key already added",
188+
transaction: null,
189+
};
181190
}
182-
// if not added, send tx to add the session key
183-
191+
// if not added or needs to be updated, send tx to add the session key
184192
const tx = await sendAndConfirmTransaction({
185193
account: options.account,
186194
transaction: addSessionKey({
187195
account: options.account,
188196
contract: accountContract,
189197
sessionKeyAddress: options.sessionKeySignerAddress,
190-
// hard coded for now
191-
permissions: {
192-
approvedTargets:
193-
typeof options.permissions["contracts:write"] === "boolean"
194-
? "*"
195-
: options.permissions["contracts:write"],
196-
permissionEndTimestamp: options.permissions.expiration,
197-
nativeTokenLimitPerTransaction: options.permissions["native:spend"],
198-
},
198+
permissions: newPermissions,
199199
}),
200200
});
201201

packages/thirdweb/test/globalSetup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,8 @@ export default async function globalSetup() {
131131
// await shutdownOdyssey();
132132
};
133133
}
134+
135+
process.on("unhandledRejection", (reason) => {
136+
console.error(`Vitest unhandledRejection: ${reason}`);
137+
throw reason;
138+
});

packages/thirdweb/test/reactSetup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ import { afterEach } from "vitest";
55
afterEach(() => {
66
cleanup();
77
});
8+
9+
process.on("unhandledRejection", (reason) => {
10+
console.error(`Vitest unhandledRejection: ${reason}`);
11+
throw reason;
12+
});

0 commit comments

Comments
 (0)