fix(react): switch chains for SIWE (#4047)

## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on updating authentication logic to switch to the proper chain before signing payloads.

### Detailed summary
- Updated `getLoginPayload` to include `chainId` parameter
- Added logic to switch to the correct chain before signing payloads
- Updated initialization of `thirdwebAuth` to handle chain switching
- Imported `getCachedChain` function in `useSiweAuth.ts`

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
This commit is contained in:
gregfromstl
2024-08-12 05:34:36 +00:00
parent cb4b6fde85
commit 7a68e3b8ed
4 changed files with 15 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---
Switch to the proper chain prior to signing SIWE payloads
@@ -9,6 +9,7 @@ const privateKey = process.env.THIRDWEB_ADMIN_PRIVATE_KEY;
const thirdwebAuth = createAuth({
domain: process.env.NEXT_PUBLIC_THIRDWEB_AUTH_DOMAIN || "",
client: THIRDWEB_CLIENT,
adminAccount: privateKey
? privateKeyToAccount({ client: THIRDWEB_CLIENT, privateKey })
: await generateAccount({ client: THIRDWEB_CLIENT }),
@@ -16,7 +16,8 @@ export function AuthButton() {
auth={{
isLoggedIn: (address) => isLoggedIn(address),
doLogin: (params) => login(params),
getLoginPayload: ({ address }) => generatePayload({ address }),
getLoginPayload: ({ address }) =>
generatePayload({ address, chainId: 84532 }),
doLogout: () => logout(),
}}
/>
@@ -1,6 +1,7 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { LoginPayload } from "../../../../auth/core/types.js";
import type { VerifyLoginPayloadParams } from "../../../../auth/core/verify-login-payload.js";
import { getCachedChain } from "../../../../chains/utils.js";
import type { Account, Wallet } from "../../../../wallets/interfaces/wallet.js";
/**
@@ -95,6 +96,12 @@ export function useSiweAuth(
import("../../../../auth/core/sign-login-payload.js"),
]);
if (payload.chain_id) {
await activeWallet.switchChain(
getCachedChain(Number(payload.chain_id)),
);
}
const signedPayload = await signLoginPayload({
payload,
account: activeAccount,