Core kit with RN and Firebase not logging in

Hi, I am using this guide to set up SFA core kit with firebase and React Native on Mac OS Ventura 13. The default setup works when running npm run ios.

Next, I added my android and ios firebase .plist and .json file and in the app.tsx file began configuring it according to the guide as well as adding the custom authentication from firebase. I have enabled email/password and google sign in.

import React, {useEffect, useState} from 'react';
import {
  Button,
  ScrollView,
  StyleSheet,
  Text,
  View,
  Dimensions,
  ActivityIndicator,
} from 'react-native';
import '@ethersproject/shims';
// IMP START - Auth Provider Login
import auth from '@react-native-firebase/auth';
// IMP END - Auth Provider Login
import EncryptedStorage from 'react-native-encrypted-storage';
import {decode as atob} from 'base-64';
import {IProvider} from '@web3auth/base';

// IMP START - Quick Start
import Web3Auth from '@web3auth/single-factor-auth-react-native';
import {EthereumPrivateKeyProvider} from '@web3auth/ethereum-provider';
// IMP END - Quick Start
import {ethers} from 'ethers';

// IMP START - Dashboard Registration
const clientId =
  'BIWIRTZ8Ep9JRKJH318N8Bsgv2JN79etAVI6xjrZf6Nt5WxxXYD-OZPRzUxLNtcBHULdgla2s8Rqozpz0xGhQs0'; // get from https://dashboard.web3auth.io
// IMP END - Dashboard Registration

// IMP START - Verifier Creation
const verifier = 'w3a-firebase-react-native';
// IMP END - Verifier Creation

// IMP START - Auth Provider Login
async function signInWithEmailPassword() {
  try {
    const res = await auth().signInWithEmailAndPassword(
      'custom+jwt@firebase.login',
      'Testing@123',
    );
    return res;
  } catch (error) {
    console.error(error);
  }
}
// IMP END - Auth Provider Login

// IMP START - SDK Initialization
const chainConfig = {
  chainId: '0x1', // Please use 0x1 for Mainnet
  rpcTarget: 'https://rpc.ankr.com/eth',
  displayName: 'Ethereum Mainnet',
  blockExplorer: 'https://etherscan.io/',
  ticker: 'ETH',
  tickerName: 'Ethereum',
};

const web3auth = new Web3Auth(EncryptedStorage, {
  clientId: 'BIWIRTZ8Ep9JRKJH318N8Bsgv2JN79etAVI6xjrZf6Nt5WxxXYD-OZPRzUxLNtcBHULdgla2s8Rqozpz0xGhQs0', // Get your Client ID from Web3Auth Dashboard
  web3AuthNetwork: 'sapphire_mainnet',
});

const privateKeyProvider = new EthereumPrivateKeyProvider({
  config: {chainConfig},
});
// IMP END - SDK Initialization

export default function App() {
  const [provider, setProvider] = useState<IProvider | null>(null);
  const [loggedIn, setLoggedIn] = useState<boolean>(false);
  const [loading, setLoading] = useState<boolean>(false);
  const [userInfo, setUserInfo] = useState<string>('');
  const [consoleUI, setConsoleUI] = useState<string>('');

  useEffect(() => {
    const init = async () => {
      try {
        // IMP START - SDK Initialization
        await web3auth.init(privateKeyProvider);
        setProvider(web3auth.provider);
        // IMP END - SDK Initialization

        if (web3auth.connected) {
          setLoggedIn(true);
        }
      } catch (error) {
        uiConsole(error, 'mounted caught');
      }
    };
    init();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const parseToken = (token: any) => {
    try {
      const base64Url = token.split('.')[1];
      const base64 = base64Url.replace('-', '+').replace('_', '/');
      return JSON.parse(atob(base64 || ''));
    } catch (err) {
      uiConsole(err);
      return null;
    }
  };

  const login = async () => {
    try {
      setConsoleUI('Logging in');
      setLoading(true);
      // IMP START - Auth Provider Login
      const loginRes = await signInWithEmailPassword();
      // IMP END - Auth Provider Login
      uiConsole('Login success', loginRes);
      // IMP START - Login
      const idToken = await loginRes!.user.getIdToken(true);
      // IMP END - Login
      uiConsole('idToken', idToken);
      const parsedToken = parseToken(idToken);
      setUserInfo(parsedToken);

      // IMP START - Login
      const verifierId = parsedToken.sub;
      await web3auth!.connect({
        verifier:'w3a-firebase-react-native', // e.g. `web3auth-sfa-verifier` replace with your verifier name, and it has to be on the same network passed in init().
        verifierId, // e.g. `Yux1873xnibdui` or `name@email.com` replace with your verifier id(sub or email)'s value.
        idToken,
      });
      // IMP END - Login
      setProvider(web3auth.provider);

      setLoading(false);
      if (web3auth.connected) {
        setLoggedIn(true);
        uiConsole('Logged In');
      }
    } catch (e) {
      uiConsole(e);
      setLoading(false);
    }
  };

  // IMP START - Blockchain Calls
  const getAccounts = async () => {
    setConsoleUI('Getting account');
    // For ethers v5
    // const ethersProvider = new ethers.providers.Web3Provider(this.provider);
    const ethersProvider = new ethers.BrowserProvider(provider!);

    // For ethers v5
    // const signer = ethersProvider.getSigner();
    const signer = await ethersProvider.getSigner();

    // Get user's Ethereum public address
    const address = signer.getAddress();
    uiConsole(address);
  };
  const getBalance = async () => {
    setConsoleUI('Fetching balance');
    // For ethers v5
    // const ethersProvider = new ethers.providers.Web3Provider(this.provider);
    const ethersProvider = new ethers.BrowserProvider(provider!);

    // For ethers v5
    // const signer = ethersProvider.getSigner();
    const signer = await ethersProvider.getSigner();

    // Get user's Ethereum public address
    const address = signer.getAddress();

    // Get user's balance in ether
    // For ethers v5
    // const balance = ethers.utils.formatEther(
    // await ethersProvider.getBalance(address) // Balance is in wei
    // );
    const balance = ethers.formatEther(
      await ethersProvider.getBalance(address), // Balance is in wei
    );

    uiConsole(balance);
  };
  const signMessage = async () => {
    setConsoleUI('Signing message');
    // For ethers v5
    // const ethersProvider = new ethers.providers.Web3Provider(this.provider);
    const ethersProvider = new ethers.BrowserProvider(provider!);

    // For ethers v5
    // const signer = ethersProvider.getSigner();
    const signer = await ethersProvider.getSigner();
    const originalMessage = 'YOUR_MESSAGE';

    // Sign the message
    const signedMessage = await signer.signMessage(originalMessage);
    uiConsole(signedMessage);
  };
  // IMP END - Blockchain Calls

  const logout = async () => {
    // IMP START - Logout
    web3auth.logout();
    // IMP END - Logout
    setProvider(null);
    setLoggedIn(false);
    setUserInfo('');
  };

  const uiConsole = (...args: any) => {
    setConsoleUI(JSON.stringify(args || {}, null, 2) + '\n\n\n\n' + consoleUI);
    console.log(...args);
  };

  const loggedInView = (
    <View style={styles.buttonArea}>
      <Button title="Get User Info" onPress={() => uiConsole(userInfo)} />
      <Button title="Get Accounts" onPress={() => getAccounts()} />
      <Button title="Get Balance" onPress={() => getBalance()} />
      <Button title="Sign Message" onPress={() => signMessage()} />
      <Button title="Log Out" onPress={logout} />
    </View>
  );

  const unloggedInView = (
    <View style={styles.buttonArea}>
      <Button title="Login with Web3Auth" onPress={login} />
      {loading && <ActivityIndicator />}
    </View>
  );

  return (
    <View style={styles.container}>
      {loggedIn ? loggedInView : unloggedInView}
      <View style={styles.consoleArea}>
        <Text style={styles.consoleText}>Console:</Text>
        <ScrollView style={styles.consoleUI}>
          <Text>{consoleUI}</Text>
        </ScrollView>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: 50,
    paddingBottom: 30,
  },
  consoleArea: {
    margin: 20,
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1,
  },
  consoleUI: {
    flex: 1,
    backgroundColor: '#CCCCCC',
    color: '#ffffff',
    padding: 10,
    width: Dimensions.get('window').width - 60,
  },
  consoleText: {
    padding: 10,
  },
  buttonArea: {
    flex: 2,
    alignItems: 'center',
    justifyContent: 'space-around',
    paddingBottom: 30,
  },
});

This is error I am receiving when running the simulator:
LOG Login success undefined
LOG [TypeError: Cannot read property 'user' of undefined]

What could be the issue?

Thanks

  "//IMP START": "IMP START - Web3Auth Installation",
  "dependencies": {
    "@ethersproject/shims": "^5.7.0",
    "@react-native-firebase/app": "^17.5.0",
    "@react-native-firebase/auth": "^17.5.0",
    "@web3auth/ethereum-provider": "^7.3.2",
    "@web3auth/single-factor-auth-react-native": "^2.1.0",
    "ethers": "^6.11.1",
    "react": "18.2.0",
    "react-native": "0.72.1",
    "react-native-encrypted-storage": "^4.0.3"
  },
  "//IMP END": "IMP END - Web3Auth Installation",

@Trueman Your issue is under review and we will get back with further updates.

Hi Vjgee, is it possible to have an update soon as I am stuck and waiting on this to proceed further? Thanks!

Any updates? The lack of product support is concerning especially at the early stages.

Hey @Trueman,

Navigating the setup of Firebase login within a React Native application can be a bit of a journey, given the intricate nature of mobile development environments. With React Native, this is a common experience in the development community, so you’re definitely not alone if you’re finding some steps less than straightforward.

I hope you’ve been able to follow through with the initial setup required for Firebase login integration as detailed here. The documentation provided aims to streamline the process, offering a step-by-step approach to get you up and running smoothly.

However, should you encounter any of the notorious metro errors—which can sometimes be cryptic and less informative—the guide available here is designed to demystify these common hurdles. It’s crafted to provide actionable solutions, specifically catering to the nuances of React Native’s build and deployment process.

If, after utilizing these resources, you find yourself still facing issues, please do not hesitate to reach out. Providing as much detail as possible about the error messages or behavior you’re experiencing will greatly assist in pinpointing the issue. Our community is here to support you through these challenges, leveraging collective knowledge to find a resolution.

We’re committed to helping you navigate these obstacles, ensuring a smoother development experience.

Thank you for your reply, I have used the guides provided. I believe the issues I am facing are regarding these 2 default parameters:

What do I enter for signInWithEmailAndPassword text?

What do I enter for verifierId, idToken?

Below is my dashboard:

Hey @Trueman,

Apologies for the delay in my response. I hope this information is still relevant to you. Regarding your query, the two default parameters for the auth().signInWithEmailAndPassword() method are indeed the email and password. These credentials are configured in our Firebase console to match the expected input. In our quickstart example, we’ve simplified the process by hardcoding these parameters, demonstrating how values typically passed from state variables related to user input are accepted.

Furthermore, your selection of verifierId as sub on the dashboard, alongside using parsedToken.sub as the verifierId in your code, is correct. This alignment means there should be no issue on this front.

I hope this clarifies your concerns. If you have further questions or need more assistance, please don’t hesitate to ask.