Flutter:检查 OTP 身份验证后是现有用户还是新用户

Flutter: Check if existing user or new user after OTP Authentication

我正在尝试在 OTP 流程之后检查用户是存在于我的 firebase 中的返回用户还是新用户,如果是,则应转到他在其中输入详细信息的页面。

我已经让用户在验证 OTP 后进入聊天屏幕,但我希望它首先检查用户是否存在,以便在他继续进入聊天屏幕之前获得额外的详细信息。

这是我使用的认证功能

  Future<void> phoneSignIn(
    BuildContext context,
    String phoneNumber,
  ) async {
    TextEditingController codeController = TextEditingController();
    try {
      // FOR ANDROID, IOS
      final newUser = await _auth.verifyPhoneNumber(
        phoneNumber: phoneNumber,
        timeout: const Duration(seconds: 120),
        //  Automatic handling of the SMS code
        verificationCompleted: (PhoneAuthCredential credential) async {
          // !!! works only on android !!!
          await _auth.signInWithCredential(credential);
        },
        // Displays a message when verification fails
        verificationFailed: (e) {
          showSnackBar(context, e.message!);
        },
        // Displays a dialog box when OTP is sent
        codeSent: ((String verificationId, int? resendToken) async {
          showOTPDialog(
            codeController: codeController,
            context: context,
            confirmOTP: () async {
              PhoneAuthCredential credential = PhoneAuthProvider.credential(
                verificationId: verificationId,
                smsCode: codeController.text.trim(),
              );
              

              // !!! Works only on Android, iOS !!!
              await _auth.signInWithCredential(credential);
              Navigator.of(context).pop(); // Remove the dialog box
            },
          );
        }),
        codeAutoRetrievalTimeout: (String verificationId) {
          // Auto-resolution timed out...
        },
      );
    } catch (e) {
      print(e);
    }
  }

这是输入收到的 OTP 代码的 OTP 简单对话框


void showOTPDialog({
  required BuildContext context,
  required TextEditingController codeController,
  required VoidCallback confirmOTP,
}) {
  showDialog(
    context:context,
    barrierDismissible: false,
    builder: (context)=> AlertDialog(
      title: const Text("Enter OTP"),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children:<Widget>[
          TextField(
            controller: codeController,
          )
        ]
      ),
      actions: <Widget>[
        TextButton(
          child:const Text('Confirm Code'),
          onPressed: confirmOTP,
        )
      ],
    ),

  );
}```

你可以这样做

auth.verifyPhoneNumber(
    phoneNumber: phoneNumber,
    verificationCompleted: (PhoneAuthCredential credential) async {
      final userCredential = await auth.signInWithCredential(credential);
      final isNew = userCredential.additionalUserInfo?.isNewUser ?? false;
      print('isNew $isNew'); // This is where you will get the value
    },
    verificationFailed: (FirebaseAuthException e) {},
    codeSent: onOtpSentHandler,
    codeAutoRetrievalTimeout: (String verificationId) {},
  );