繁体   English   中英

Flutter:检查OTP认证后是现有用户还是新用户

[英]Flutter: Check if existing user or new user after OTP Authentication

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

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

这是我使用的身份验证 function。

  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) {},
  );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM