Flutter + Firebase 身份验证 - 在第二个身份验证实例中添加用户时出现 JSObject 错误

时间:2021-02-16 08:47:00

标签: firebase flutter dart firebase-authentication flutter-web

这是我第一次在这里发布疑问以寻求帮助,因此提前感谢您的任何回复。

我是 Flutter 的新手,但我已经在后端数据库网站上工作了几个月,以创建/编辑授权用户、获取数据并对其进行可视化。

和我之前的其他人一样,我正在尝试使用 Firebase Authentication 添加新用户而无需自己注销。目前,我对使用 Firebase Admin SDK 不感兴趣 - 我已经尝试过失败,目前我更喜欢使用 Firebase Authentication 工具。

因此,我按照 Flutter: Firebase Authentication Create User Without Automatically Logging In 中提到的说明使用新的 FirebaseAuth instance 创建用户。然而,这个解决方案对我来说并不完美,因为第一次一切都很好,但没有后续。

您可以在下面看到我用来创建带有 createUserWithEmailAndPassword() 的用户的代码片段,您会发现我在其中创建了第二个 Auth() instance,并在使用后将其删除。虽然第一次运行完美并且没有返回任何异常,但第二次等等都会在 UserCredential userCredetial 赋值时返回错误。

Future<String> createNewUser(String email) async {
   
    String newUserUid = "";

    //random password generation
    var randomPassword = Random.secure();
    const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789|!"@·#\$~%&¬/()=?\'[]{}<>-_*+,;.:';
    int length = 10;
    String initPassword = String.fromCharCodes(Iterable.generate((length), (_) => chars.codeUnitAt(randomPassword.nextInt(chars.length))));

    //secondary app creation
    FirebaseApp tempApp = await Firebase.initializeApp(name: 'Temporary', options: Firebase.app().options);
    print('First part Ok: ${tempApp.name} created');

    try{
    //user creation with email and password
    UserCredential userCredential = await FirebaseAuth.instanceFor(app: tempApp).createUserWithEmailAndPassword(email: email, password: initPassword); //HERE IT IS THE PROBLEM
    newUserUid = userCredential.user.uid; 
    } catch(error) {
      print('It is failing at this point: $error');
    }
    //secondary app deletion after use
    await tempApp.delete().then((value) => print('Second part Ok: ${tempApp.name} deleted')).catchError((value) => print('Second part NOk: tempApp not deleted'));

    return newUserUid;
  }

第二次用这个方法新建用户,返回这个错误:

It is failing at this point: Expected a value of type 'JSObject<I>', but got one of type 'NativeJavaScriptObject

在第一次调用和下一次调用之间我没有改变任何东西,我也不使用构造函数 UserCredential.fromJsObject(UserCredentialJsImpl jsObject) 来生成 class UserCredetential。这个错误对我来说毫无意义,因为我可以看到用户已在 Firebase 控制台中使用他们自己的 uid 成功创建,但是 userCredential 存在一些问题,我无法识别并使方法返回 {{1}此错误后的值。我还应该补充一点,我已经检查过 null 是否已正确创建和删除 - 显然确实如此。

就是这样。如果有人可以看看这个,我真的很感激。

快乐编码!

0 个答案:

没有答案