Error firebase auth invalid credential when trying to generate a token for push notifications

Widgets & Design

Hello, I am using the following code to generate token for push notifications:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

Future<String?> createUserCode(String email, String password) async {
  String? fcmToken;

  try {
    // Attempt to sign in with the provided credentials
    UserCredential userCredential =
        await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: email,
      password: password,
    );

    // If sign-in is successful, retrieve FCM token
    fcmToken = await FirebaseMessaging.instance.getToken();
    debugPrint("FCM Token saved: $fcmToken");
    FFAppState().update(() {
      FFAppState().fcmToken = fcmToken ?? 'null';
    });

    return fcmToken;
  } on FirebaseAuthException catch (e) {
    if (e.code == 'user-not-found') {
      // Create a new user if not found
      try {
        UserCredential userCredential =
            await FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: email,
          password: password,
        );

        // Retrieve FCM token for the newly created user
        fcmToken = await FirebaseMessaging.instance.getToken();
        debugPrint("FCM Token saved: $fcmToken");
        FFAppState().update(() {
          FFAppState().fcmToken = fcmToken ?? 'null';
        });

        return fcmToken;
      } on FirebaseAuthException catch (e) {
        if (e.code == 'weak-password') {
          debugPrint('The password provided is too weak.');
          FFAppState().update(() {
            FFAppState().errorMessage = 'The password provided is too weak.';
          });
        } else if (e.code == 'email-already-in-use') {
          debugPrint('The account already exists for that email.');
          FFAppState().update(() {
            FFAppState().errorMessage =
                'The account already exists for that email.';
          });
        } else {
          debugPrint(e.toString());
          FFAppState().update(() {
            FFAppState().errorMessage = e.toString();
          });
        }
      } catch (e) {
        debugPrint(e.toString());
        FFAppState().update(() {
          FFAppState().errorMessage = e.toString();
        });
      }
    } else if (e.code == 'wrong-password') {
      debugPrint('Wrong password provided for that user.');
      FFAppState().update(() {
        FFAppState().errorMessage = 'Wrong password provided for that user.';
      });
    } else if (e.code == 'invalid-credential') {
      // Handle invalid credentials error explicitly
      debugPrint('Invalid email or password.');
      FFAppState().update(() {
        FFAppState().errorMessage = 'Invalid email or password.';
      });
    } else {
      debugPrint(e.toString());
      FFAppState().update(() {
        FFAppState().errorMessage = e.toString();
      });
    }
  } catch (e) {
    debugPrint(e.toString());
    FFAppState().update(() {
      FFAppState().errorMessage = e.toString();
    });
  }

  return fcmToken;
}

the code works for some accounts, but for one account it generates the error

[firebase_auth/invalid-credential] The supplied auth credential is incorrect, malformed or has expired.

What is going on? Why am I getting this error? How can I fix the issue?

What have you tried so far?

making it work

Did you check FlutterFlow's Documentation for this topic?
Yes
1