Error: undefined is not an object (evaluating 'connectedChainId.toString')

When I use the mobile version this error appears, however if I close it I can access the application. how can I remove this error?

When using a desktop browser this does not happen.

ERRO :

Uncaught runtime errors:

×

ERROR

undefined is not an object (evaluating 'connectedChainId.toString') @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_ethereum-provider_dist_ethereumProv-b48506.index_bundle.js:12521:58 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_ethereum-provider_dist_ethereumProv-b48506.index_bundle.js:12499:53 emit@https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/index_bundle.js:85357:17 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:41125 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:41321 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:29524 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:29636 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:29131 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:29134 @https://3u94satfnhf713scffigb9td6c.ingress.akt.yatara.be/vendors-node_modules_web3auth_modal_node_modules_web3auth_wallet-connect-v2-adapter_dist_wall-3021c4.index_bundle.js:2759:56453

Hey @maury1418,

I hope you are doing well! Could you please update all our packages to the latest version, v8.5.0?

If the error persists, please share some code snippets with us so we can provide more in-depth help.

I updated the version and made adjustments according to the documentation. However, I noticed that wallets like Metamask are no longer appearing. Do I need to make any additional configurations?

import React, { useCallback, useEffect, useState, ReactNode } from 'react'
import { ADAPTER_EVENTS, CHAIN_NAMESPACES, CONNECTED_EVENT_DATA, UserInfo, WEB3AUTH_NETWORK } from "@web3auth/base";
import { Web3Auth } from '@web3auth/modal';
import { LOGIN_MODAL_EVENTS } from "@web3auth/ui"
import Web3 from "web3";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";

const WEB3AUTH_CLIENT_ID = process.env.REACT_APP_WEB3_KEY || "";

export interface AuthProviderData {
    web3auth: Web3Auth | null,
    provider: CONNECTED_EVENT_DATA | null,
    user: Partial<UserInfo> | null,
    address: string | null,
    isLogin: boolean;
    onSuccessfulLogin: (data: CONNECTED_EVENT_DATA, user: Partial<UserInfo>, address: string) => void,
    login: () => void,
    logout: () => void,
    cleanIsLogin: () => void,
}

export const AuthProviderContext = React.createContext<AuthProviderData>({
    web3auth: null,
    provider: null,
    user: null,
    address: null,
    isLogin: false,
    onSuccessfulLogin: (data: any) => { },
    login: () => { },
    logout: () => { },
    cleanIsLogin: () => { },
});

const chainConfig = {
    chainId: "0x1", // Please use 0x1 for Mainnet
    rpcTarget: "https://rpc.ankr.com/eth",
    chainNamespace: CHAIN_NAMESPACES.EIP155,
    displayName: "Ethereum Mainnet",
    blockExplorerUrl: "https://etherscan.io/",
    ticker: "ETH",
    tickerName: "Ethereum",
    logo: "https://images.toruswallet.io/eth.svg",
    uiConfig: {
        appName: "iPal",
        mode: "auto"
    },
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
    config: { chainConfig: chainConfig }
});

const web3auth = new Web3Auth({
    clientId: WEB3AUTH_CLIENT_ID,
    web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
    privateKeyProvider: privateKeyProvider,
});


// const web3auth = new Web3Auth({
//     clientId: WEB3AUTH_CLIENT_ID,
//     web3AuthNetwork: "sapphire_devnet",
//     chainConfig: {
//         chainId: "0x1",
//         rpcTarget: "https://rpc.ankr.com/eth",
//         chainNamespace: CHAIN_NAMESPACES.EIP155,
//         displayName: "Ethereum Mainnet",
//         blockExplorer: "https://etherscan.io/",
//         ticker: "ETH",
//         tickerName: "Ethereum",
//     },
//     uiConfig: {
//         appName: "iPal",
//         mode: "auto"
//     },
// });

export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
    const [provider, setProvider] = useState<CONNECTED_EVENT_DATA | null>(null);
    const [user, setUser] = useState<Partial<UserInfo> | null>(null);
    const [address, setAddress] = useState<string | null>(null);
    const [isLogin, setIsLogin] = useState<boolean>(false);

    const onSuccessfulLogin = useCallback((data: CONNECTED_EVENT_DATA, user: Partial<UserInfo>, address: string) => {
        // console.log('onSuccessfulLogin', data, user, address);
        setProvider(data);
        setUser(user);
        setAddress(address);
    }, [])

    const cleanIsLogin = () => {
        setIsLogin(false);
    };

    const login = useCallback(() => {
        web3auth.connect().then(() => {
            setIsLogin(true);
        }).catch(err => {
            console.log(err)
        })
    }, [])

    const logout = useCallback(() => {
        setUser(null);
        setProvider(null);
        setAddress(null);
        web3auth.logout().then(() => {
            // login on logout

        }).catch(err => {
            console.log('logout', err)
        })
    }, [])

    const subscribeAuthEvents = useCallback((web3auth: Web3Auth) => {
        web3auth.on(ADAPTER_EVENTS.CONNECTED, (data: CONNECTED_EVENT_DATA) => {
            // console.log("Yeah!, you are successfully logged in", data);

            const web3 = new Web3(web3auth.provider as any);
            web3.eth.getAccounts().then((address) => {

                // GET BALANCE
                // web3.eth.getBalance(address[0]).then((value) => {
                //     const balance = web3.utils.fromWei(value ,"ether");
                //     console.log("balance: ", balance);
                // })

                web3auth.getUserInfo().then((user) => {
                    onSuccessfulLogin(data, user, address[0])
                })
            })
        });

        web3auth.on(ADAPTER_EVENTS.CONNECTING, () => {
            console.log("connecting");
        });

        web3auth.on(ADAPTER_EVENTS.DISCONNECTED, () => {
            console.log("disconnected");
            web3auth.clearCache();
            setUser(null);
            setProvider(null);
            setAddress(null);
            localStorage.clear();
        });

        web3auth.on(ADAPTER_EVENTS.ERRORED, (error) => {
            console.log("some error or user have cancelled login request", error);
        });

        web3auth.on(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, (isVisible) => {
            console.log("modal visibility", isVisible);
        });

    }, [onSuccessfulLogin])

    useEffect(() => {
        subscribeAuthEvents(web3auth);

        web3auth.initModal().catch(err => {
            alert('error' + err)
        })
        console.log(web3auth);
    }, [])

    const ctx: AuthProviderData = {
        web3auth,
        provider,
        user,
        address,
        isLogin,
        cleanIsLogin,
        onSuccessfulLogin,
        login,
        logout,
    }
    return (
        <AuthProviderContext.Provider value={ctx}>
            {children}
        </AuthProviderContext.Provider>
    )
}

export const AuthConsumer = AuthProviderContext.Consumer

I do it and its working

import React, { useCallback, useEffect, useState, ReactNode } from 'react'
import { ADAPTER_EVENTS, CHAIN_NAMESPACES, CONNECTED_EVENT_DATA, UserInfo, WEB3AUTH_NETWORK } from "@web3auth/base";
import { Web3Auth } from '@web3auth/modal';
import { LOGIN_MODAL_EVENTS } from "@web3auth/ui"
import Web3 from "web3";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import { MetamaskAdapter } from "@web3auth/metamask-adapter";

const WEB3AUTH_CLIENT_ID = process.env.REACT_APP_WEB3_KEY || "";

export interface AuthProviderData {
    web3auth: Web3Auth | null,
    provider: CONNECTED_EVENT_DATA | null,
    user: Partial<UserInfo> | null,
    address: string | null,
    isLogin: boolean;
    onSuccessfulLogin: (data: CONNECTED_EVENT_DATA, user: Partial<UserInfo>, address: string) => void,
    login: () => void,
    logout: () => void,
    cleanIsLogin: () => void,
}

export const AuthProviderContext = React.createContext<AuthProviderData>({
    web3auth: null,
    provider: null,
    user: null,
    address: null,
    isLogin: false,
    onSuccessfulLogin: (data: any) => { },
    login: () => { },
    logout: () => { },
    cleanIsLogin: () => { },
});

const chainConfig = {
    chainId: "0x1", // Please use 0x1 for Mainnet
    rpcTarget: "https://rpc.ankr.com/eth",
    chainNamespace: CHAIN_NAMESPACES.EIP155,
    displayName: "Ethereum Mainnet",
    blockExplorerUrl: "https://etherscan.io/",
    ticker: "ETH",
    tickerName: "Ethereum",
    logo: "https://images.toruswallet.io/eth.svg"
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
    config: { chainConfig: chainConfig }
});

const web3auth = new Web3Auth({
    clientId: WEB3AUTH_CLIENT_ID,
    web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
    privateKeyProvider: privateKeyProvider,
    uiConfig: {
        appName: "iPal",
        mode: "auto"
    },
});



const metamaskAdapter = new MetamaskAdapter({
    clientId: WEB3AUTH_CLIENT_ID,
    sessionTime: 3600, // 1 hour in seconds
    web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
    chainConfig: {
        chainNamespace: CHAIN_NAMESPACES.EIP155,
        chainId: "0x1",
        rpcTarget: "https://rpc.ankr.com/eth", // This is the public RPC we have added, please pass on your own endpoint while creating an app
    },
});

// it will add/update  the metamask adapter in to web3auth class
web3auth.configureAdapter(metamaskAdapter);

// You can change the adapter settings by calling the setAdapterSettings() function on the adapter instance.
metamaskAdapter.setAdapterSettings({
    sessionTime: 86400, // 1 day in seconds
    chainConfig: {
        chainNamespace: CHAIN_NAMESPACES.EIP155,
        chainId: "0x1",
        rpcTarget: "https://rpc.ankr.com/eth", // This is the public RPC we have added, please pass on your own endpoint while creating an app
    },
    web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
});



// const web3auth = new Web3Auth({
//     clientId: WEB3AUTH_CLIENT_ID,
//     web3AuthNetwork: "sapphire_devnet",
//     chainConfig: {
//         chainId: "0x1",
//         rpcTarget: "https://rpc.ankr.com/eth",
//         chainNamespace: CHAIN_NAMESPACES.EIP155,
//         displayName: "Ethereum Mainnet",
//         blockExplorer: "https://etherscan.io/",
//         ticker: "ETH",
//         tickerName: "Ethereum",
//     },
//     uiConfig: {
//         appName: "iPal",
//         mode: "auto"
//     },
// });

export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
    const [provider, setProvider] = useState<CONNECTED_EVENT_DATA | null>(null);
    const [user, setUser] = useState<Partial<UserInfo> | null>(null);
    const [address, setAddress] = useState<string | null>(null);
    const [isLogin, setIsLogin] = useState<boolean>(false);

    const onSuccessfulLogin = useCallback((data: CONNECTED_EVENT_DATA, user: Partial<UserInfo>, address: string) => {
        // console.log('onSuccessfulLogin', data, user, address);
        setProvider(data);
        setUser(user);
        setAddress(address);
    }, [])

    const cleanIsLogin = () => {
        setIsLogin(false);
    };

    const login = useCallback(() => {
        web3auth.connect().then(() => {
            setIsLogin(true);
        }).catch(err => {
            console.log(err)
        })
    }, [])

    const logout = useCallback(() => {
        setUser(null);
        setProvider(null);
        setAddress(null);
        web3auth.logout().then(() => {
            // login on logout

        }).catch(err => {
            console.log('logout', err)
        })
    }, [])

    const subscribeAuthEvents = useCallback((web3auth: Web3Auth) => {
        web3auth.on(ADAPTER_EVENTS.CONNECTED, (data: CONNECTED_EVENT_DATA) => {
            // console.log("Yeah!, you are successfully logged in", data);

            const web3 = new Web3(web3auth.provider as any);
            web3.eth.getAccounts().then((address) => {

                // GET BALANCE
                // web3.eth.getBalance(address[0]).then((value) => {
                //     const balance = web3.utils.fromWei(value ,"ether");
                //     console.log("balance: ", balance);
                // })

                web3auth.getUserInfo().then((user) => {
                    onSuccessfulLogin(data, user, address[0])
                })
            })
        });

        web3auth.on(ADAPTER_EVENTS.CONNECTING, () => {
            console.log("connecting");
        });

        web3auth.on(ADAPTER_EVENTS.DISCONNECTED, () => {
            console.log("disconnected");
            web3auth.clearCache();
            setUser(null);
            setProvider(null);
            setAddress(null);
            localStorage.clear();
        });

        web3auth.on(ADAPTER_EVENTS.ERRORED, (error) => {
            console.log("some error or user have cancelled login request", error);
        });

        web3auth.on(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, (isVisible) => {
            console.log("modal visibility", isVisible);
        });

    }, [onSuccessfulLogin])

    useEffect(() => {
        subscribeAuthEvents(web3auth);

        web3auth.initModal().catch(err => {
            alert('error' + err)
        })
        console.log(web3auth);
    }, [])

    const ctx: AuthProviderData = {
        web3auth,
        provider,
        user,
        address,
        isLogin,
        cleanIsLogin,
        onSuccessfulLogin,
        login,
        logout,
    }
    return (
        <AuthProviderContext.Provider value={ctx}>
            {children}
        </AuthProviderContext.Provider>
    )
}

export const AuthConsumer = AuthProviderContext.Consumer```

@TomTom when I try to log in on my cell phone through in browser, the metamask option does not appear only on desktop, can you help me?

I implemented these two examples, however it doesn’t work to open the metamask on cell, not even on desktop

the only example that works is this, but it doesn’t work on mobile:

hi @maury1418

Could you please try our EVM Modal example?

Demo: https://evm-modal-example.vercel.app/
Github: web3auth-pnp-examples/web-modal-sdk/blockchain-connection-examples/evm-modal-example at main · Web3Auth/web3auth-pnp-examples · GitHub

In this example, I can see Metamask in the web and mobile environment.

Please contact me if you have any other questions.

1 Like

Hi @TomTom now it work on mobile, however, when I click on metamask instead of opening the wallet it opens the app store to install

Note: for this code to work I had to Enable WalletConnect V2 on dashboard

import React, { ReactNode, createContext, useContext, useEffect, useState } from "react";
import { CHAIN_NAMESPACES, IProvider, WEB3AUTH_NETWORK, UserInfo } from "@web3auth/base";
import { Web3Auth, Web3AuthOptions } from "@web3auth/modal";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";

// Providers
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";

// Wallet Services
import { WalletServicesPlugin } from "@web3auth/wallet-services-plugin";

// Adapters
import { getDefaultExternalAdapters } from "@web3auth/default-evm-adapter";

const clientId = process.env.REACT_APP_WEB3_KEY || "";

const chainConfig = {
    chainId: "0x1", // Please use 0x1 for Mainnet
    rpcTarget: "https://eth.drpc.org",
    chainNamespace: CHAIN_NAMESPACES.EIP155,
    displayName: "Ethereum Mainnet",
    blockExplorerUrl: "https://etherscan.io/",
    ticker: "ETH",
    tickerName: "Ethereum",
    logo: "https://images.toruswallet.io/eth.svg"
};

const privateKeyProvider = new EthereumPrivateKeyProvider({
    config: { chainConfig: chainConfig }
});

const web3AuthOptions: Web3AuthOptions = {
    clientId,
    web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
    privateKeyProvider: privateKeyProvider,
    sessionTime: 86400, // 1 day
};

export interface Web3AuthContextProps {
    web3auth: Web3Auth | null,
    loggedIn: boolean,
    getUserInfo: () => void,
    login: () => void,
    logout: () => void,
}

const Web3AuthContext = createContext<Web3AuthContextProps | undefined>(undefined);

export const Web3AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
    const [web3auth, setWeb3auth] = useState<Web3Auth | null>(null);
    const [walletServicesPlugin, setWalletServicesPlugin] = useState<WalletServicesPlugin | null>(null);
    const [loggedIn, setLoggedIn] = useState(false);


    useEffect(() => {
        const init = async () => {
            try {
                const web3auth = new Web3Auth(web3AuthOptions);

                const openloginAdapter = new OpenloginAdapter({
                    loginSettings: {
                        mfaLevel: "optional",
                    },
                    adapterSettings: {
                        uxMode: "popup", // "redirect" | "popup"
                        mfaSettings: {
                            deviceShareFactor: {
                                enable: true,
                                priority: 1,
                                mandatory: true,
                            },
                            backUpShareFactor: {
                                enable: true,
                                priority: 2,
                                mandatory: true,
                            },
                            socialBackupFactor: {
                                enable: true,
                                priority: 3,
                                mandatory: false,
                            },
                            passwordFactor: {
                                enable: true,
                                priority: 4,
                                mandatory: false,
                            },
                        },
                    },
                });
                web3auth.configureAdapter(openloginAdapter);

                // Wallet Services Plugin
                const walletServicesPlugin = new WalletServicesPlugin({
                    walletInitOptions: {
                        whiteLabel: {
                            showWidgetButton: true,
                            buttonPosition: "bottom-left",
                        }
                    }
                });
                setWalletServicesPlugin(walletServicesPlugin);
                web3auth.addPlugin(walletServicesPlugin);

                // read more about adapters here: https://web3auth.io/docs/sdk/pnp/web/adapters/

                // Only when you want to add External default adapters, which includes WalletConnect, Metamask, Torus EVM Wallet
                const adapters = await getDefaultExternalAdapters({ options: web3AuthOptions });

                adapters.forEach((adapter) => {
                    web3auth.configureAdapter(adapter);
                });


                setWeb3auth(web3auth);

                await web3auth.initModal();

                if (web3auth.connected) {
                    setLoggedIn(true);
                }
            } catch (error) {
                console.error(error);
            }
        };

        init();
    }, []);

    const login = async () => {
        if (!web3auth) {
            console.log("web3auth not initialized yet");
            return;
        }
        await web3auth.connect();
    };

    const getUserInfo = async () => {
        if (!web3auth) {
            console.log("web3auth not initialized yet");
            return;
        }
        const user = await web3auth.getUserInfo();
        return user;
    };

    const logout = async () => {
        if (!web3auth) {
            console.log("web3auth not initialized yet");
            return;
        }
        await web3auth.logout();
        setLoggedIn(false);
    };

    const ctx: Web3AuthContextProps = {
        web3auth,
        loggedIn,
        getUserInfo,
        login,
        logout,
    };
    return (
        <Web3AuthContext.Provider value={ctx}>
            {children}
        </Web3AuthContext.Provider>
    )
};

export const useWeb3Auth = (): Web3AuthContextProps => {
    const context = useContext(Web3AuthContext);
    if (!context) {
        throw new Error("useWeb3Auth must be used within a Web3AuthProvider");
    }
    return context;
};


Hi @maury1418,

If you haven’t installed it yet, you need to do so. It’s working fine on my iOS. If it’s not working on yours, please share your iOS version and update Metamask to the latest version on your phone.

As you can see in the video in the link, the version is updated.

https://1drv.ms/v/s!AkifBRUDsnUOho06lfVLEJpSej9tAA

Even on Android it’s not working

I updated the dependencies thinking it might be a version but it still didn’t work.

                "@web3auth/base": "^8.6.1",
                "@web3auth/default-evm-adapter": "^8.6.1",
                "@web3auth/ethereum-provider": "^8.6.1",
                "@web3auth/metamask-adapter": "^8.6.1",
                "@web3auth/modal": "^8.6.1",
                "@web3auth/openlogin-adapter": "^8.6.1",
                "@web3auth/wallet-services-plugin": "^8.6.1",

I tested it in that demo and I can’t authenticate with metamask on mobile
Demo: https://evm-modal-example.vercel.app/

iPHONE 11 - IOS Version: 17.4.1
Samsung Galaxy S9 - Android Version: 10

Hi @maury1418,

I can’t reproduce the issue, so let me check with the team and get back to you.

1 Like

This forum was working on this same problem, I think the problem is with WalletConnectV2Adapter

Hey @maury1418,

Can you please share the repo for you app?
I’ll personally have a look and let you know.

1 Like

Hi @maharshi ,

The project I’m working on is closed-source, so I can’t share the repository directly. However, I’ve created a separate repository with the same versions of the libraries installed in the original project and included how I am using the authentication.

You can access the repository here: web3auth-test.

I appreciate you taking a look and I’m available for any additional information you might need.

Thank you!

Hi @maury1418,

Is this issue fixed? I noticed you were using v8.6. Please update to the latest version, as all these issues have been resolved in the latest SDK.

Let me know if the problem persists.