【发布时间】:2021-12-28 08:02:20
【问题描述】:
我正在为德国客户开发一款移动应用。我在斯里兰卡。 OTP 在我的国家运行良好,但不适用于其他国家。我试图从斯里兰卡输入德国电话号码,它工作正常。当我试图从斯里兰卡向他的手机发送 OTP 时,他会收到 OTP 代码。但是,当他在德国尝试时,它不起作用。
然后,我连接到德国VPN并尝试,我也未能获得OTP。然后,我连接了其他国家的VPN并尝试了。这是行不通的。它仅在我连接到斯里兰卡网络时才有效。
我很困惑,我是否需要在 Firebase 中打开任何东西才能在全球范围内发送 OTP。请尝试向我提供您的答案。
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Login"),),
body: const MyApp(),
),
);
}
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var otpController = TextEditingController();
var numController = TextEditingController();
FirebaseAuth auth = FirebaseAuth.instance;
String verificationId = "";
void signInWithPhoneAuthCredential(
PhoneAuthCredential phoneAuthCredential) async {
try {
final authCredential =
await auth.signInWithCredential(phoneAuthCredential);
if (authCredential.user != null) {
print("Welcome");
Text("Welcome");
}
} on FirebaseAuthException catch (e) {
print("catch");
}
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(10),
child: TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Phone Number',
hintText: 'Enter valid number'
),
controller: numController,
),
),
Padding(
padding: EdgeInsets.all(10),
child: TextField(
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
hintText: 'Enter valid password'
),
controller: otpController,
),
),
TextButton(onPressed: () {
fetchotp();
}, child: const Text("Fetch OTP"),),
TextButton(onPressed: () {
verify();
}, child: const Text("Send"))
],
),
);
}
Future<void> verify() async {
PhoneAuthCredential phoneAuthCredential =
PhoneAuthProvider.credential(
verificationId: verificationId, smsCode: otpController.text);
signInWithPhoneAuthCredential(phoneAuthCredential);
}
Future<void> fetchotp() async {
await auth.verifyPhoneNumber(
phoneNumber: '+94769008291',
verificationCompleted: (PhoneAuthCredential credential) async {
await auth.signInWithCredential(credential);
},
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
print('The provided phone number is not valid.');
}
},
codeSent: (String verificationId, int? resendToken) async {
this.verificationId = verificationId;
},
codeAutoRetrievalTimeout: (String verificationId) {
},
);
}
}
【问题讨论】:
标签: android firebase flutter firebase-authentication