颤振电话号码验证

时间:2018-08-31 00:35:42

标签: firebase dart firebase-authentication flutter

am尝试使用下面链接中帖子Here中Jayesh Chawla的电话号码对用户进行身份验证 但是它对我不起作用。 这是我的功能,可以进行电话身份验证

     signUpUserPN()async{
      final PhoneVerificationCompleted verificationCompleted = (FirebaseUser user){
        setState(() {
                      message = Future<String>.value("auto sign in succedded $user");
                      print("Sign up succedded");
                      _pref.setString("phonkey",user.phoneNumber.toString());
                      //MyNavigator.goToDetail(context);
//called when the otp is variefied automatically
                    });
      };
  final PhoneVerificationFailed verificationFailed = (AuthException authException){
    setState(() {
                  message = Future<String>.value("verification failed code: ${authException.code}. Message: ${authException.message}");
                });
  };

  final PhoneCodeSent codeSent =(String dverificationId,[int forceResendingToken]) async {
    my1verificationId= dverificationId;
 };

  final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout = (String dverificationId){
    my1verificationId = dverificationId;
  };

  await _auth.verifyPhoneNumber(
    phoneNumber: '+233557340556',
    timeout: Duration(seconds: 60),
    verificationCompleted: verificationCompleted,
    verificationFailed: verificationFailed,
    codeSent: codeSent,
    codeAutoRetrievalTimeout: codeAutoRetrievalTimeout
    );
print("$message");
print("$my1verificationId");
}

1 个答案:

答案 0 :(得分:0)

我创建了一个util类来使用电话号码对用户进行身份验证。使用前,请确保已在Android和Ios中设置了Firebase,并且Firebase声明了here,并且已从Firebase项目面板启用了电话号码身份验证。在下面查看util类:

import 'dart:async';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/services.dart';
import 'package:flutter_firebase/network/networ_util.dart';

class FirebasePhoneUtil {
  static final FirebasePhoneUtil _instance = new FirebasePhoneUtil.internal();

  FirebasePhoneUtil.internal();

  factory FirebasePhoneUtil() {
    return _instance;
  }

  FirebaseAuthListener _view;

  final FirebaseAuth _auth = FirebaseAuth.instance;
  var api = new NetworkUtil();

  String verificationId;

  setScreenListener(FirebaseAuthListener view) {
    _view = view;
  }

//  FirebasePhoneUtil(this._view);

  Future<void> verifyPhoneNumber(String phoneNumber, String code) async {
    final PhoneVerificationCompleted verificationCompleted =
        (FirebaseUser user) {};

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {};

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      this.verificationId = verificationId;
      _view.verificationCodeSent(forceResendingToken);
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {};

    await _auth.verifyPhoneNumber(
        phoneNumber: code + phoneNumber,
        timeout: const Duration(seconds: 5),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

  verifyOtp(String smsCode) async {
    final FirebaseUser user = await _auth
        .signInWithPhoneNumber(
          verificationId: verificationId,
          smsCode: smsCode,
        )
        .then((FirebaseUser res) {
      identical("", "");
    })
        .catchError(
            (PlatformException error) => onTokenError(error.toString()));
  }
}

onTokenError(String string) {
  print("ssss");
}

abstract class FirebaseAuthListener {
  verificationCodeSent(int forceResendingToken);
}