Autheticate with Web3Auth and change chain id programmatically

Hey

The app uses Web3Auth and Polygon for authentication, but then I need to switch the chain to Ethereum, I’m doing this programmatically and it works fine because I can switch the chain in Metamask

`import { useMoralis } from “react-moralis”;
.
.
.
const { provider } = useMoralis();

if (provider) {
try {
await provider.request({
jsonrpc: “2.0”,
method: “wallet_switchEthereumChain”,
params: [
{
chainId: “0x1”,
},
],
});
} catch (error) {
console.error(error);
}
}`

But I have a useEffect to watch the chainId from Moralis, and it’s 0x1 for a few seconds and then changes to 0x87 Polygon.

Why does the chainId change if in my Metamask is Ethereum set?

Is there another way to change the chain using web3auth as an authenticator?

I think you have to sign a new message using Moralis.authenticate with chain Id as 0x1
https://v1docs.moralis.io/moralis-dapp/users/web3-login/web3auth#parameters

Not sure if using metamask method: “wallet_switchEthereumChain” updates the chain in web3auth.

Also, try using v2 sdk for authentication so that you can use react hooks directly from web3auth sdk.

@johnversus is there any way to avoid losing the user session? just change the chain.

I’ve tried to use authenticate({ provider: "web3Auth", clientId: process.env.GATSBY_WEB3AUTH_CLIENT_ID, chainId: Moralis.Chains.ETH_MAINET, });
when I need to change the chain to Ethereum, but this opens the web3auth modal again, and I supposed that the user needs to log in again, but in the Ethereum chain

when I need to change the chain to Ethereum, but this opens the web3auth modal again

If you don’t logout, you can change the current chain (using the chainId parameter) and call authenticate() again without needing to login again (through the Web3Auth modal).