flutter - 仅在使用电话号码的颤振应用中首次进行身份验证
问题描述
我是移动应用程序开发的新手。我正在尝试构建一个要求用户使用电话号码注册的应用程序。我正在使用 Firebase 进行身份验证。我已经能够使用电话号码注册用户。但是,我希望应用程序第一次注册用户,并在进一步加载时,应该显示主页。此外,如果用户在移动设备中更改了他的电话号码,那么他应该再次被要求注册新的电话号码。我在这里尝试过一些东西......
class _State extends State<MyAppPage> {
String phoneNo;
String smsOTP;
String verificationId;
String errorMessage = '';
FirebaseAuth _auth = FirebaseAuth.instance;
Future<void> verifyPhone() async {
final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) {
this.verificationId = verId;
smsOTPDialog(context).then((value) {
print('Sign in');
});
};
try {
await _auth.verifyPhoneNumber(
phoneNumber: this.phoneNo,
timeout: const Duration(seconds:20),
verificationCompleted: (AuthCredential phoneAuthCredential) {
print(phoneAuthCredential);
},
verificationFailed: (AuthException exceptio) {
print('${exceptio.message}');
},
codeSent: smsOTPSent,
codeAutoRetrievalTimeout: (String verId) {
this.verificationId = verId;
});
} catch(e) {
handleError(e);
}
}
Future<bool> smsOTPDialog(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return new AlertDialog(
title: Text('Enter SMS Code'),
content: Container(
height: 85,
child: Column(children: [
TextField(
onChanged: (value) {
this.smsOTP = value;
},
),
(errorMessage != ''
? Text(
errorMessage,
style: TextStyle(color: Colors.red),
)
: Container()
)
]),
),
contentPadding: EdgeInsets.all(10),
actions: <Widget>[
FlatButton(
child: Text('Done'),
onPressed: () {
_auth.currentUser().then((user) {
if(user != null) {
Navigator.of(context).pop();
Navigator.of(context).pushReplacementNamed('/homepage');
}
else {
signIn();
}
});
},
)
],
);
}
);
}
signIn() async {
try {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: smsOTP,
);
final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
Navigator.of(context).pop();
Navigator.of(context).pushReplacementNamed('/homepage');
}
catch (e) {
handleError(e);
}
}
handleError(PlatformException error) {
print(error);
switch (error.code) {
case 'ERROR_INVALID_VERIFICATION_CODE':
FocusScope.of(context).requestFocus(new FocusNode());
setState(() {
errorMessage = 'Invalid Code';
});
Navigator.of(context).pop();
smsOTPDialog(context).then((value) {
print('Sign In');
});
break;
default:
setState(() {
errorMessage = error.message;
});
break;
}
}
用户界面如下..
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Phone Authentication'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(10),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter Phone Number Eg. +910000000000'),
onChanged: (value) {
this.phoneNo = value;
},
),
),
(errorMessage != ''
? Text(
errorMessage,
style: TextStyle(color: Colors.red),
)
: Container()),
SizedBox(
height: 10,
),
RaisedButton(
onPressed: () {
verifyPhone();
},
child: Text('Verify'),
textColor: Colors.white,
elevation: 7,
color: Colors.blue,
)
],
),
),
);
}
问题是firebase首次使用设备中的电话号码对新用户进行身份验证,但在重新打开应用程序时再次询问电话号码。此外,在提供电话号码时,firebase 没有发送 OTP,因为用户已经过身份验证。我会很感激你的帮助。提前致谢..
解决方案
推荐阅读
- python - 可以优化给定的代码吗,我尝试通过引用不同的站点来做到这一点,但它总是向我显示错误
- java - Comparing interface variable without cast to Enum value in java
- python - 将时间序列与 merge_asof 合并并保持最长的时间范围(外连接)
- javascript - 如何在单个文件中导入所有全局 Vue 组件
- excel - 在选定的工作表中循环相同的宏代码
- architecture - 即插即用系统设计?
- excel-formula - 如何锁定工作簿中所有工作表的第 1 行和不受限制的剩余单元格
- javascript - 遍历对象数组
- php - 有没有一种简单的方法可以减少 AWS PHP SDK 以仅使用 S3?
- php - 我尝试使用 ajax 在 laravel 中搜索。我发布了两个变量,但我的查询不起作用这是我的代码