Reputation: 547
I am trying to integrate authentication on Flutter using firebase_auth
.
However, whenever I call the verifyPhoneNumber("+256XXXXXXXXXX")
I get the error message A network error (such as timeout, interrupted connection or unreachable host) has occurred.
, that is from the PhoneVerificationFailed
callback. An for that reason cannot get the SMS.
I have tried;
Adding network permissions as seen below to my file (my internet connection works, as I am able to Google via the emulator)
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Checking the validity of my API keys
I am totally confused as to why Flutter is unable to communicate with firebase. I have two questions.
My implimentatioin is as below;
import 'package:firebase_auth/firebase_auth.dart';
FirebaseAuth auth = FirebaseAuth.instance;
var message;
// fire this when Phone verification is completed
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) {
auth.signInWithCredential(phoneAuthCredential);
message = 'Received phone auth credential: $phoneAuthCredential';
print(message);
};
// fire this when Phone verification fails
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
message =
'Phone verification failed. Code: ${authException.code}. Message: ${authException.message}';
print(message);
};
// fire this when SMS code is sent is sent.
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
verificationId = verificationId;
print('Sent verification code');
};
// fire this when smsCode expires
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
verificationId = verificationId;
print('Auto retrival time-out');
};
// verify phone number
verifyPhoneNumber(String phoneNumber) {
auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
timeout: const Duration(seconds: 30),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
print('Verification Initiated');
}
// sign in with phone.
signInWithPhoneNumber(String smsCode, String verificationId) async {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: smsCode,
);
final FirebaseUser user = (await auth.signInWithCredential(credential)).user;
final FirebaseUser currentUser = await auth.currentUser();
assert(user.uid == currentUser.uid);
if (user != null) {
message = 'Successfully signed in, uid: ' + user.uid;
} else {
message = 'Sign in failed';
}
}
Upvotes: 24
Views: 26787
Reputation: 325
In my case, the solution was super simple. I'm using the Firebase emulator, and I forgot to start it. After starting the emulator, obviously, it worked fine.
Upvotes: 0
Reputation: 131
network error (such as timeout interrupted connection or unreachable host) has ocurred
In my case the error occurred because I had configured to use the auth emulator, but it was turned off.
Upvotes: 2
Reputation: 13529
I had an issue trying to access the Internet via deploying to macos in debug mode. Turns out it was entitlements that were not set in:
/Users/sparkyspider/StudioProjects/Helicopters/macos/Runner/DebugProfile.entitlements
See below on how to do it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
<!-- ADD THIS HERE TO ALLOW YOUR CLIENT INTERNET ACCESS -->
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
Upvotes: 4
Reputation: 381
Normally when the emulator is doing a lot of work on the thread, it tends to misbehave, such a losing internet connectivity
even if your PC is well connected.
My suggestion(Which worked for me) is you kill the emulator, and go to Android Studio AVD Manager
and Wipe Data
for that Emulator, then restart the Emulator, it worked for me.
Upvotes: 5
Reputation:
in my case I have disabled the wifi. Once I enabled the wifi on the phone everything worked as expected.
Upvotes: 0
Reputation: 199
use the latest mobile emulators it works for me. before I used the Google Pixel 2 API emulator. I got the same error using this device. after I upgraded my emulator Google Pixel 7 error was gone.
Upvotes: 0
Reputation: 713
I had the same error, in my case I could just solve it with:
flutter clean
Upvotes: 0
Reputation: 365
"I am late to the party but I found another solution. If using an android emulator, disabling the Android Studio HTTP proxy settings can restore the network connection." - MaximeBeasse
This network error has stopped me too and I have been seeking an answer ... After reading MaximeBeasse's response above and trying it out, I found that it worked : ) Authentication now works! Finally I can move on!
Many thanks @MaximeBeasse -- salute his answer please
Upvotes: 1
Reputation: 21
I am late to the party but I found another solution.
If using an android emulator, disabling the Android Studio HTTP proxy settings can restore the network connection.
Upvotes: 2
Reputation:
Open Terminal
in the project folder, run the following two commands:
adb kill-server
adb start-server
If adb
is not recognized, add C:\Users\USERNAME\AppData\Local\Android\Sdk\platform-tools
to environement variables.
Upvotes: 0
Reputation: 2165
import 'package:badam/varify.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:async';
import 'HomePage.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'FireBase Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: new LoginPage(),
routes: <String, WidgetBuilder>{
'/loginpage' : (BuildContext context) => Dash(),
'/landpage' : (BuildContext context) => LoginPage(),
}
);
}
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
String phoneNo, smsId, verificationId;
Future<void> verifyPhone() async{
final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId){
this.verificationId = verId;
};
final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]){
this.verificationId = verId;
smsCodeDialoge(context).then((value){
print('Signed In');
});
};
final PhoneVerificationCompleted verifiedSuccess = (AuthCredential auth){
print('verified');
};
final PhoneVerificationFailed verifyFailed = (AuthException e) {
print('${e.message}');
};
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNo,
timeout: const Duration(seconds: 5),
verificationCompleted: verifiedSuccess,
verificationFailed: verifyFailed,
codeSent: smsCodeSent,
codeAutoRetrievalTimeout: autoRetrieve,
);
}
Future<bool> smsCodeDialoge(BuildContext context){
return showDialog(context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return new AlertDialog(
title: Text('Enter OTP'),
content: TextField(
onChanged: (value) {
this.smsId = value;
},
),
contentPadding: EdgeInsets.all(10.0),
actions: <Widget>[
new FlatButton(
onPressed: (){
FirebaseAuth.instance.currentUser().then((user){
if(user != null){
Navigator.of(context).pop();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Dash()),
);
}
else{
Navigator.of(context).pop();
signIn(smsId);
}
}
);
},
child: Text('Done', style: TextStyle( color: Colors.blue),))
],
);
},
);
}
Future<void> signIn(String smsCode) async {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: smsCode,
);
await FirebaseAuth.instance.signInWithCredential(credential)
.then((user){
Navigator.of(context).pushReplacementNamed('/loginpage');
}).catchError((e){
print(e);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sign In')
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Phone Auth',style: TextStyle(fontSize: 20,color: Colors.blue),),
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter your phone number',
),
onChanged: (value){
this.phoneNo = value;
},
),
),
SizedBox(height: 10.0),
RaisedButton(
onPressed: verifyPhone,
child: Text('Verify', style: TextStyle(color: Colors.white),),
elevation: 7.0,
color: Colors.blue,
)
],
),
);
}
}
It worked for me, hope this helps you!
Upvotes: -3
Reputation: 61
if you are trying this out in a simulator, then this error may pop up as the internet of your simulator is not connected. To solve this you can run your app on a physical device and it will work!
Upvotes: 5
Reputation: 571
In my case I solved allowing Outgoing Connections on the Xcode's Runner.xcworkspace
Upvotes: 29
Reputation: 1036
In my case, my VPN was causing the problem. Disabling the VPN and testing it again solved the error. I hope it helps.
Upvotes: 9