MPC Core Kit SDK not working for social auths

Hello,
We have an application in which we are implementing authentication. We are using web3Auth MPC core kit for it. We are having google, twitter and discord social auths for login.

The issue we are facing is that the social auth logins are very inconsistent or do not work at all and throw errors.

I am attaching my codebase foe your reference, please let me know what could be the fix for this.

import { ethers } from "ethers";
import { useEffect, useState } from "react";
import { Web3AuthMPCCoreKit, WEB3AUTH_NETWORK } from "@web3auth/mpc-core-kit";

const uiConsole = (...args: any[]): void => {
  const el = document.querySelector("#console>p");
  if (el) {
    el.innerHTML = JSON.stringify(args || {}, null, 2);
  }
  console.log(...args);
};

function App() {
  const [coreKitInstance, setCoreKitInstance] =
    useState<Web3AuthMPCCoreKit | null>(null);
  const [provider, setProvider] = useState<any>(null);

  useEffect(() => {
    const init = async () => {
      // Initialization of Service Provider
      try {
        const coreKitInstance = new Web3AuthMPCCoreKit({
          web3AuthClientId: process.env
            .NEXT_PUBLIC_WEB3AUTH_CLIENT_ID as string,
          web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET,
          uxMode: "popup",
        });
        await coreKitInstance.init();
        setCoreKitInstance(coreKitInstance);
        if (coreKitInstance.provider) setProvider(coreKitInstance.provider);
      } catch (error) {
        console.error(error);
      }
    };
    init();
  }, []);

  const googleLogin = async () => {
    if (!coreKitInstance) {
      uiConsole("coreKitInstance not initialized yet");
      return;
    }
    try {
      const provider = await coreKitInstance.connect(
        {
          subVerifierDetails: {
            typeOfLogin: "google",
            verifier: google verifier name,
            clientId:clientId,
          },
        }
      );

      if (provider) setProvider(provider);
    } catch (error) {
      if ((error as Error).message === "required more shares") {
        resetAccount();
      }
      uiConsole(error);
    }
  };

  const discordLogin = async () => {
    if (!coreKitInstance) {
      uiConsole("coreKitInstance not initialized yet");
      return;
    }
    try {
      const provider = await coreKitInstance.connect({
        subVerifierDetails: {
          typeOfLogin: "discord",
          verifier: discord verifier name,
          clientId: clientId,
        },
      });

      if (provider) setProvider(provider);
    } catch (error) {
      if ((error as Error).message === "required more shares") {
        resetAccount();
      }
      uiConsole(error);
    }
  };

  const twitterLogin = async () => {
    if (!coreKitInstance) {
      uiConsole("coreKitInstance not initialized yet");
      return;
    }
    try {
      const provider = await coreKitInstance.connect({
        subVerifierDetails: {
          typeOfLogin: "twitter",
          verifier: twitter verifier name,
          clientId: clientId,
        },
      });

      if (provider) setProvider(provider);
    } catch (error) {
      if ((error as Error).message === "required more shares") {
        resetAccount();
      }
      uiConsole(error);
    }
  };

  const logout = async () => {
    if (!coreKitInstance) {
      uiConsole("coreKitInstance not initialized yet");
      return;
    }
    uiConsole("Log out");
    await coreKitInstance.logout();
    setProvider(null);
  };

  const resetAccount = async (): Promise<void> => {
    if (!coreKitInstance) {
      throw new Error("coreKitInstance is not set");
    }
    await coreKitInstance.CRITICAL_resetAccount();
    uiConsole("reset account successful");
  };

  const getUserInfo = () => {
    const user = coreKitInstance?.getUserInfo();
    uiConsole(user);
  };

  const getKeyDetails = () => {
    const keyDetails = coreKitInstance?.getKeyDetails();
    uiConsole(keyDetails);
  };

  const getChainID = async () => {
    if (!provider) {
      console.log("provider not initialized yet");
      return;
    }
    // eslint-disable-next-line  @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const ethersProvider = new ethers.providers.Web3Provider(provider);
    const { chainId } = await ethersProvider.getNetwork();
    uiConsole(chainId);
    return chainId;
  };

  const getAccounts = async () => {
    if (!provider) {
      console.log("provider not initialized yet");
      return;
    }
    // eslint-disable-next-line  @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const ethersProvider = new ethers.providers.Web3Provider(provider);
    const address = (await ethersProvider.listAccounts())[0];

    uiConsole(address);
    return address;
  };

  const getBalance = async () => {
    if (!provider) {
      console.log("provider not initialized yet");
      return;
    }
    // eslint-disable-next-line  @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const ethersProvider = new ethers.providers.Web3Provider(provider);
    const address = (await ethersProvider.listAccounts())[0];
    const balance = ethers.utils.formatEther(
      await ethersProvider.getBalance(address)
    );
    uiConsole(balance);
    return balance;
  };

  const signMessage = async (): Promise<any> => {
    if (!provider) {
      console.log("provider not initialized yet");
      return;
    }
    // eslint-disable-next-line  @typescript-eslint/ban-ts-comment
    // @ts-ignore
    const ethersProvider = new ethers.providers.Web3Provider(provider);

    const address = (await ethersProvider.listAccounts())[0];

    const signer = ethersProvider.getSigner(address);

    const message = "message";

    const signature = await signer.signMessage(message);

    uiConsole(signature);
  };

  const sendTransaction = async () => {
    if (!provider) {
      console.log("provider not initialized yet");
      return;
    }

    try {
      // eslint-disable-next-line  @typescript-eslint/ban-ts-comment
      // @ts-ignore
      const ethersProvider = new ethers.providers.Web3Provider(provider);
      const fromAddress = (await ethersProvider.listAccounts())[0];

      const params = [
        {
          from: fromAddress,
          to: "0x732A45Ea1d54C8457f1Ec8738b477364C57cC3ca",
          value: ethers.utils.parseEther("0.0001").toHexString(),
        },
      ];

      const transactionHash = await ethersProvider.send(
        "eth_sendTransaction",
        params
      );

      uiConsole(transactionHash);
    } catch (error) {
      console.error(error);
    }
  };

  const loggedInView = (
    <>
      <h2 className="subtitle">Account Details</h2>
      <div className="flex-container">
        <button onClick={getUserInfo} className="card">
          Get User Info
        </button>
        <br />

        <button onClick={getKeyDetails} className="card">
          Get Key Details
        </button>
        <br />

        <button onClick={logout} className="card">
          Log Out
        </button>
        <br />
      </div>

      <div className="flex-container">
        <button onClick={getChainID} className="card">
          Get Chain ID
        </button>
        <br />

        <button onClick={getAccounts} className="card">
          Get Accounts
        </button>
        <br />

        <button onClick={getBalance} className="card">
          Get Balance
        </button>
        <br />

        <button onClick={signMessage} className="card">
          Sign Message
        </button>
        <br />

        <button onClick={sendTransaction} className="card">
          Send Transaction
        </button>
        <br />
      </div>

      <div id="console" style={{ whiteSpace: "pre-line" }}>
        <p style={{ whiteSpace: "pre-line" }}></p>
      </div>
    </>
  );

  const unloggedInView = (
    <>
      <button onClick={() => googleLogin()}>google</button>
      <br />
      <button onClick={() => twitterLogin()}>twitter</button>
      <br />
      <button onClick={() => discordLogin()}>discord</button>
    </>
  );

  return (
    <div>
      <div>{provider ? loggedInView : unloggedInView}</div>
    </div>
  );
}

export default App;

@shubbham.jain Thanks for your recent post.

Could you elaborate on the errors you receive? Can you share the console logs ?

Hi @vjgee,

For twitter we always get this error-

For discord, we get different errors, I have attached image of one of those errors.

Or sometimes we get error related to tkey.

Let me know if this helps or not.

@shubbham.jain Here is the update from our team:

  • For Twitter, you need to pass a domain like auth0. More information below:
  • For Discord, its because discord issues same oauth token for sometime and we need to clear the token everytime we login. More information below:

Hi @vjgee,

We appreciate the quick response.

Regarding the twitter fix that you highlighted, we looked into it and are not sure about it. When we were setting up social auth for twitter, we created a auth0 verifier and provided auth0 clientId and domain. Do we need to do something else as well, let us know that. If you can provide code example it will make it clear a lot more.

Regarding the discord fix that you highlighted, we looked into that as well. We are not sure as to where we will get access to the discord token on the frontend where we are setting up these auths. Could let us know what should we change in the above code provided to get discord working.

Some other errors to highlight that we are getting-

We also get this error for all the social auths from time to time, we are not sure why this keeps coming up.

We also get this error for google social auth somethimes.

Let us know what could be the fix for all these errors.

Thank you.

Please be sure to be on sapphire-mainnet - not devnet as devnet undergoes development from time to time

@zhen @vjgee ,
Sorry for responding after a long time.

We did try the fixes that were pointed out, but none of it seems to help a whole lot.

Google and discord social auths are working but the accuracy is very low.

Twitter social auth does not work, we tried experimenting and changing things around but nothing seems to work.

Please help us in solving the issue as soon as possible as it is very important that it works for our application.

Thank you.

@shubbham.jain Are you still facing any difficulties?

Do join our upcoming community call - Web3Auth Community Call #4 · Zoom · Luma