如何使用firebase_auth(电话身份验证)在Flutter应用中添加短信自动验证?

时间:2019-08-08 08:35:04

标签: flutter dart firebase-authentication

在我的Flutter应用中,我正在使用 firebase 电话身份验证进行登录。在所有方面,它都运行良好。通过使用当前代码,我必须在收到代码时显式添加 sms 代码。

如何在Flutter应用中自动进行短信验证?

    Future<void> _sendCodeToPhoneNumber() async {

    final String phone = country + phoneNumberController.text;

    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential credential) {
      setState(() {
        print(
            'Inside _sendCodeToPhoneNumber: signInWithPhoneNumber auto succeeded: $credential');
      });
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      setState(() {
        print(
            'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
      });
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      this._verificationId = verificationId;
      print("code sent to " + phone);
      setState(() {
        this.status = AuthStatus.SMS_AUTH;
      });
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      this._verificationId = verificationId;

      print("time out");
    };

    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: phone,
        timeout: const Duration(seconds: 1),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

4 个答案:

答案 0 :(得分:1)

在onPhoneVerificationCompleted中编写以下内容:

Building native extensions. This could take a while...
ERROR:  Error installing re2:
    ERROR: Failed to build gem native extension.

    current directory: /home/aaron/.rvm/gems/ruby-2.5.3/gems/re2-1.1.1/ext/re2
/home/aaron/.rvm/rubies/ruby-2.5.3/bin/ruby -I /home/aaron/.rvm/rubies/ruby-2.5.3/lib/ruby/site_ruby/2.5.0 -r ./siteconf20200629-12178-1dryx90.rb extconf.rb
checking for -lstdc++... yes
checking for stdint.h... yes
checking for rb_str_sublen()... yes
checking for -lre2... yes
checking for re2 requires C++11 compiler... yes
checking for RE2::Match() with endpos argument... yes
creating Makefile

current directory: /home/aaron/.rvm/gems/ruby-2.5.3/gems/re2-1.1.1/ext/re2
make "DESTDIR=" clean

current directory: /home/aaron/.rvm/gems/ruby-2.5.3/gems/re2-1.1.1/ext/re2
make "DESTDIR="
compiling re2.cc
cc1plus: warning: command line option ‘-Wimplicit-int’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wdeclaration-after-statement’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wimplicit-function-declaration’ is valid for C/ObjC but not for C++ [enabled by default]
In file included from /home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby/ruby.h:29:0,
                 from /home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby.h:33,
                 from re2.cc:9:
/home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby/defines.h:286:44: warning: invalid suffix on literal; C++11 requires a space between literal and identifier [-Wliteral-suffix]
 #   define EXTERN __pragma(message(__FILE__"("STRINGIZE(__LINE__)"): warning: "\
                                            ^
In file included from /home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby/intern.h:24:0,
                 from /home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby/ruby.h:2040,
                 from /home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby.h:33,
                 from re2.cc:9:
/home/aaron/.rvm/rubies/ruby-2.5.3/include/ruby-2.5.0/ruby/defines.h:286:44: warning: invalid suffix on literal; C++11 requires a space between literal and identifier [-Wliteral-suffix]
 #   define EXTERN __pragma(message(__FILE__"("STRINGIZE(__LINE__)"): warning: "\
                                            ^
re2.cc: In function ‘VALUE re2_scanner_scan(VALUE)’:
re2.cc:261:37: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
               p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1"));
                                     ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_matchdata_to_a(VALUE)’:
re2.cc:459:35: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
             p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1"));
                                   ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_matchdata_nth_match(int, VALUE)’:
re2.cc:483:33: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
           p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                                 ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_matchdata_inspect(VALUE)’:
re2.cc:614:29: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
       p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                             ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_regexp_initialize(int, VALUE*, VALUE)’:
re2.cc:687:19: error: ‘class re2::RE2::Options’ has no member named ‘set_utf8’
       re2_options.set_utf8(RTEST(utf8));
                   ^
re2.cc: In function ‘VALUE re2_regexp_inspect(VALUE)’:
re2.cc:770:29: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
       p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                             ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_regexp_to_s(VALUE)’:
re2.cc:788:29: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
       p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                             ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_regexp_utf8(VALUE)’:
re2.cc:818:42: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
   return BOOL2RUBY(p->pattern->options().utf8());
                                          ^
re2.cc:21:23: note: in definition of macro ‘BOOL2RUBY’
 #define BOOL2RUBY(v) (v ? Qtrue : Qfalse)
                       ^
re2.cc: In function ‘VALUE re2_regexp_error_arg(VALUE)’:
re2.cc:1015:31: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
         p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                               ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_regexp_options(VALUE)’:
re2.cc:1046:39: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
       BOOL2RUBY(p->pattern->options().utf8()));
                                       ^
re2.cc:21:23: note: in definition of macro ‘BOOL2RUBY’
 #define BOOL2RUBY(v) (v ? Qtrue : Qfalse)
                       ^
re2.cc: In function ‘VALUE re2_regexp_named_capturing_groups(VALUE)’:
re2.cc:1116:33: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
           p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1"),
                                 ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_Replace(VALUE, VALUE, VALUE, VALUE)’:
re2.cc:1287:31: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
         p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                               ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_GlobalReplace(VALUE, VALUE, VALUE, VALUE)’:
re2.cc:1324:31: error: ‘const class re2::RE2::Options’ has no member named ‘utf8’
         p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
                               ^
re2.cc:37:36: note: in definition of macro ‘ENCODED_STR_NEW’
       int _enc = rb_enc_find_index(encoding); \
                                    ^
re2.cc: In function ‘VALUE re2_regexp_utf8(VALUE)’:
re2.cc:819:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
At global scope:
cc1plus: warning: unrecognized command line option "-Wno-self-assign" [enabled by default]
cc1plus: warning: unrecognized command line option "-Wno-constant-logical-operand" [enabled by default]
cc1plus: warning: unrecognized command line option "-Wno-parentheses-equality" [enabled by default]
cc1plus: warning: unrecognized command line option "-Wno-tautological-compare" [enabled by default]
make: *** [re2.o] Error 1

make failed, exit code 2

Gem files will remain installed in /home/aaron/.rvm/gems/ruby-2.5.3/gems/re2-1.1.1 for inspection.
Results logged to /home/aaron/.rvm/gems/ruby-2.5.3/extensions/x86_64-linux/2.5.0/re2-1.1.1/gem_make.out

答案 1 :(得分:0)

使用sms_autofill 1.1.1

Flutter插件提供SMS代码自动填充支持。

对于iOS,不需要此程序包,因为默认情况下会提供SMS自动填充功能,但对于Android则不需要此程序包。

由于没有使用SMSRetriever API的需要,因此没有询问用户读取SMS消息的权限。

您有两个可自动填充SMS代码的窗口小部件PinFieldAutoFill和TextFieldPinAutoFill。

在将电话号码发送到后端之前,您需要告知插件它需要侦听带有代码的SMS。

要做到这一点,您需要做的:

await SmsAutoFill().listenForCode;

这将在1分钟内监听带有代码的SMS,并在收到消息后自动填充以下小部件。

有关更多信息,请查看此链接 https://pub.dev/packages/sms_autofill

答案 2 :(得分:0)

我没有使用此google sms接收器API。我认为该代码可用于自动填充短信代码https://developers.google.com/identity/sms-retriever/overview

答案 3 :(得分:-1)

我不知道我是否理解正确,您是否要自动验证SMS代码?

这仅在Android中是可能的(自动验证过程发生时,函数verifyCompleted返回对象凭证),但是您应该始终使用UI进行后备以验证SMS代码,因为Android并非总是可以自动验证短信代码。

此外,您还需要代码来生成凭据,然后执行登录/注册过程。

让我知道这是否回答了您的问题。