Below is my Phone Authentication code. On a real device, when I run my app after sending a phone number to Auth.verifyPhoneNumber()
function it crashes in 2 seconds. But it is working fine on emulator. I don't know what is going to happen in the code please help me. Sorry for the bad English I am not a native speaker.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterphoneauth/datamodel/datamodel.dart';
import 'package:toast/toast.dart';
class AuthService {
String _phoneNumber;
String _verificationId;
String _smsCode;
String _message;
final FirebaseAuth _auth = FirebaseAuth.instance;
String get phoneNumber1 => _phoneNumber;
String get verificationId => _verificationId;
String get smsCode => _smsCode;
String get message => _message;
Future<void> verifyPhoneNumber(
String phoneNumber, BuildContext context) async {
Toast.show("Run", context);
try {
PhoneVerificationCompleted verificationCompleted =
(PhoneAuthCredential phoneAuthCredential) async {
Toast.show("verification done", context);
// UserCredential _userCredential =
// await _auth.signInWithCredential(phoneAuthCredential);
};
PhoneVerificationFailed verificationFailed =
(FirebaseAuthException authException) async {
print('Auth Exception Occured');
// throw Exception(authException.message);
// _message =
// 'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
};
PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
print('SMS Sent');
_verificationId = verificationId;
};
PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) async {
print('SMS Auto Retrieval Timeout');
_verificationId = verificationId;
};
await _auth.verifyPhoneNumber(
phoneNumber: "+92$phoneNumber",
timeout: const Duration(seconds: 5),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
} catch (e) {
database.msgError = e.toString();
}
}
// Example code of how to sign in with the phone.
Future signInWithPhoneNumber(String smsCode) async {
final AuthCredential credential = PhoneAuthProvider.credential(
verificationId: _verificationId,
smsCode: smsCode,
);
return (await _auth.signInWithCredential(credential)).user;
}
}
I found a solution and you can say reason actually I try to send code again and again on the same number that's a reason firebase not send code on my real device again and again. and you can check more about of security rules on official documentation
https://firebase.google.com/docs/auth/android/phone-auth#security-concerns
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.