Skip to content

Commit 21938f9

Browse files
use shouldUpdateSessionKey in login flow (#6348)
1 parent aabdd7c commit 21938f9

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

apps/login/src/api/login/config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ export async function getLoginConfig(clientId: string): Promise<LoginConfig> {
1313
...DEFAULT_CONFIG,
1414
id: "mode_earn_app",
1515
name: "Mode Earn App",
16-
logo: "https://www.modemobile.com/",
17-
logoLink:
18-
"https://cdn.prod.website-files.com/61c25b8fda22538c7d02b8ae/64109452b73a8648ed02afee_mode-logo.svg",
16+
logo: "https://cdn.prod.website-files.com/61c25b8fda22538c7d02b8ae/64109452b73a8648ed02afee_mode-logo.svg",
17+
logoLink: "https://www.modemobile.com/",
1918
chainId: polygonAmoy.id,
2019
sessionKeySignerAddress: "0xb89e32a18350d6df5bf0b89a227E098013C4Fa72",
2120
};

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

0 commit comments

Comments
 (0)