User not found exception on Android devices

Please provide the following details too when asking for help in this category:

  • SDK Version: web3auth_flutter: ^2.0.3 and web3dart: ^2.4.1
  • Related to Custom Authentication? Please provide the following info too: (Optional)
    • Verifier Name: toad-cash-verifier
    • Sample idToken(JWT) from TorusUserInfo class : eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlRZT2dnXy01RU9FYmxhWS1WVlJZcVZhREFncHRuZktWNDUzNU1aUEMwdzAifQ.eyJpYXQiOjE2OTQ0OTkxMDQsImF1ZCI6IkJJLWxxcGFHWllDRW1CRkota09JQ0xDUXNidXd1U1hJcXlONUdQazhOMjdIMG5pM2E4VzA4a1dyRGF6LWRybWRhNjZpbUtPVFFwMHZPSlNmWkJFaDJnSSIsIm5vbmNlIjoiMDNkOGFjOTVjNTJkOWRjMWNhNzIxN2IwMzk2MzQ2YzVhODk1NzcxOTUxZTAzYTZiYWY4Nzc4YTFkZDliMDc4YzYxIiwiaXNzIjoiaHR0cHM6Ly9hcGktYXV0aC53ZWIzYXV0aC5pbyIsIndhbGxldHMiOlt7InB1YmxpY19rZXkiOiIwMmY1ZmY5MGFiNDM1NTQ5YzQzOWQyYTE4NTdlOGE2ZDYxNDRkNDI2OWQ5NDQ4NzRlM2JlYzQ5YWIzODE0MDgyOWEiLCJ0eXBlIjoid2ViM2F1dGhfYXBwX2tleSIsImN1cnZlIjoic2VjcDI1NmsxIn1dLCJlbWFpbCI6ImhhcmlzaGl2QHJvdmVyeC50ZWFtIiwibmFtZSI6IkhhcmlzaGl2IFNpbmdoIiwicHJvZmlsZUltYWdlIjoiaHR0cHM6Ly9saDMuZ29vZ2xldXNlcmNvbnRlbnQuY29tL2EvQUFjSFR0ZUhHaGlFTnRyRDFNcWtJQy1GYzRhbWwwLW1FckUtZFZMdmJXVEtIUnJtPXM5Ni1jIiwidmVyaWZpZXIiOiJ0b2FkLXRlc3QtdmVyaWZpZXIiLCJ2ZXJpZmllcklkIjoiNmN3WDRMUzNFaWVldnByZEI4eDVHamY3ekdNMiIsImFnZ3JlZ2F0ZVZlcmlmaWVyIjoidG9hZC10ZXN0LXZlcmlmaWVyIiwiZXhwIjoxNjk0NTg1NTA0fQ.MQmzN-TpTeYovKs5CHa7yzAoXtigZ1R5fGpomoI_zPXZdE4i-F-B_lTt4BUQx5WisTV0xXoHaI83WctLfq0koQ

Please provide the Web3Auth initialization and login code snippet below:

Initialisation:

  Future<void> _initPlatformState() async {
    final themeMap = HashMap<String, String>();
    themeMap['primary'] = "#F5820D";

    Uri redirectUrl;
    if (Platform.isAndroid) {
      redirectUrl = Uri.parse('w3a://com.example.app/auth');
    } else if (Platform.isIOS) {
      redirectUrl = Uri.parse('com.example.app://openlogin');
    } else {
      throw UnKnownException('Unknown platform');
    }

    final loginConfig = HashMap<String, LoginConfigItem>();
    loginConfig['jwt'] = LoginConfigItem(
      verifier: "toad-test-verifier", // get it from web3auth dashboard
      typeOfLogin: TypeOfLogin.jwt,
      name: "Custom JWT Login",
      clientId: dotenv.env['WEB3AUTH_CLIENT_ID'],
    );

    await Web3AuthFlutter.init(
      Web3AuthOptions(
        clientId: dotenv.env['WEB3AUTH_CLIENT_ID'] ?? '',
        network: Network.testnet,
        redirectUrl: redirectUrl,
        whiteLabel: WhiteLabelData(
          dark: true,
          name: "Toad Cash",
          theme: themeMap,
        ),
        loginConfig: loginConfig,
      ),
    );
  }

Login code:

  VoidCallback _login(Future<Web3AuthResponse> Function() method) {
    return () async {
      context.showProgressBar();
      try {
        final Web3AuthResponse response = await method();
        SharedPreference.setPrivateKey(response.privKey.toString());
        SharedPreference.setIdToken(
            response.userInfo?.idToken.toString() ?? '');
        UserDetails userDetails = UserDetails(
            name: response.userInfo?.name,
            email: response.userInfo?.email,
            profileImage: response.userInfo?.profileImage);
        _proceedToSuccessScreen(userDetails);
      } on UserCancelledException {
        context.dismissProgressBar();
        print("User cancelled.");
      } on UnKnownException {
        context.dismissProgressBar();
        print("Unknown exception occurred");
      }
    };
  }

  Future<Web3AuthResponse> _googleSignInWithJWT() async {
    var idToken = await _getFirebaseIdTokenFromGoogleSignIn();

    return Web3AuthFlutter.login(
      LoginParams(
        loginProvider: Provider.jwt,
        mfaLevel: MFALevel.NONE,
        extraLoginOptions: ExtraLoginOptions(
          id_token: idToken,
          domain: 'https://toad.cash',
        ),
      ),
    );
  }

  Future<String> _getFirebaseIdTokenFromGoogleSignIn() async {
    var idToken = "";
    try {
      final GoogleSignInAccount? result = await GoogleSignIn().signIn();
      final GoogleSignInAuthentication? googleAuth =
          await result?.authentication;
      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth?.accessToken,
        idToken: googleAuth?.idToken,
      );
      final userCredential =
          await FirebaseAuth.instance.signInWithCredential(credential);

      idToken = (await userCredential.user?.getIdToken(true)).toString();
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-not-found') {
        print('No user found for that email.');
      } else if (e.code == 'wrong-password') {
        print('Wrong password provided for that user.');
      }
    }
    return idToken;
  }

For the first login, I am getting the idToken successfully but when I restart the application it gives me a PlatformException for User not found.

@harishiv Thanks for reaching out.

Your issue has been forwarded to our team and we will get back once there is a meaningful update to share.

Hi @harishiv, can you list down all steps you are doing?

Hi @gaurav,

I followed the following steps:

  1. Generated idToken using FirebaseAuth
  2. Used the above idToken to login into Web3Auth using Web3AuthFlutter.login
  3. After successful login, fetched the idToken from userInfo (Web3AuthFlutter.getUserInfo())

This complete flow works fine for the first time, but when I close the app restart it again, the idTokn from Web3AuthFlutter.getUserInfo() gives me an exception of User not found. The exact exception is mentioned below:

PlatformException(error, java.lang.Error: No user found, please login again!, java.lang.Error: No user found, please login again!, null)

And the whole flow is working as expected in iOS, but this issue is only happening in android.

@harishiv Have you tried running example app with sdk, that’s working fine. Pls check code with that.

I tried to run the application but there it was giving me an error of adb: failed to install /Users/StudioProjects/web3auth-pnp-examples/flutter/flutter-firebase-example/build/app/outputs/flutter-apk/app-debug.apk: Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk]

I compared the code, and it’s similar to mine.

I was able to run the application in one your examples (https://github.com/Web3Auth/web3auth-pnp-examples/tree/main/flutter/flutter-example), but there as well as soon as I close the app and relaunch it once again. It asks me to login again.

The user needs to login on every single launch.

I am talking about the example attached with sdk repo itself i.e. https://github.com/Web3Auth/web3auth-flutter-sdk/tree/master/example