Flutter 首次使用电话号码在颤振应用程序中进行身份验证
我对手机应用开发还不熟悉。我正试图建立一个应用程序,要求用户注册的电话号码。我正在使用Firebase进行身份验证。我已经能够注册用户的电话号码。但是,我想应用程序注册用户的第一次和进一步加载后,主页应该显示。此外,如果用户在移动设备中更改了其电话号码,则应再次要求用户注册新的电话号码。 我在这里试过一些东西Flutter 首次使用电话号码在颤振应用程序中进行身份验证,flutter,firebase-authentication,Flutter,Firebase Authentication,我对手机应用开发还不熟悉。我正试图建立一个应用程序,要求用户注册的电话号码。我正在使用Firebase进行身份验证。我已经能够注册用户的电话号码。但是,我想应用程序注册用户的第一次和进一步加载后,主页应该显示。此外,如果用户在移动设备中更改了其电话号码,则应再次要求用户注册新的电话号码。 我在这里试过一些东西 class _State extends State<MyAppPage> { String phoneNo; String smsOTP; String veri
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;
}
}
class\u状态扩展状态{
字符串phoneNo;
字符串smsOTP;
字符串验证ID;
字符串errorMessage='';
FirebaseAuth _auth=FirebaseAuth.instance;
Future verifyPhone()异步{
final PhoneCodeSent smsOTPSent=(字符串verId,[int-forceCodeResend]){
this.verificationId=verId;
smsOTPDialog(上下文)。然后((值){
打印(“登录”);
});
};
试一试{
等待_auth.verifyPhoneNumber(
电话号码:this.phoneNo,
超时:常数持续时间(秒:20),
验证已完成:(AuthCredential电话AuthCredential){
打印(电话认证);
},
验证失败:(AuthException异常){
打印(“${exceptio.message}”);
},
代码发送:smsOTPSent,
codeAutoRetrievalTimeout:(字符串验证){
this.verificationId=verId;
});
}捕获(e){
handleError(e);
}
}
未来smsOTPDialog(构建上下文){
返回显示对话框(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回新警报对话框(
标题:文本(“输入短信代码”),
内容:容器(
身高:85,
子项:列(子项:[
文本字段(
一旦更改:(值){
this.smsOTP=值;
},
),
(错误消息!=''”
?文本(
错误消息,
样式:TextStyle(颜色:Colors.red),
)
:Container()
)
]),
),
contentPadding:EdgeInsets.all(10),
行动:[
扁平按钮(
子项:文本('Done'),
已按下:(){
_auth.currentUser().then((用户){
如果(用户!=null){
Navigator.of(context.pop();
Navigator.of(context.pushReplacementNamed('/homepage');
}
否则{
签名();
}
});
},
)
],
);
}
);
}
signIn()异步{
试一试{
最终AuthCredential凭据=PhoneAuthProvider.getCredential(
验证ID:verificationId,
smsCode:smsOTP,
);
最终FirebaseUser用户=(等待身份验证登录,使用凭证(凭证)).user;
final FirebaseUser currentUser=wait_auth.currentUser();
断言(user.uid==currentUser.uid);
Navigator.of(context.pop();
Navigator.of(context.pushReplacementNamed('/homepage');
}
捕获(e){
handleError(e);
}
}
handleError(平台异常错误){
打印(错误);
开关(错误代码){
案例“错误\无效\验证码”:
FocusScope.of(context).requestFocus(newfocusnode());
设置状态(){
errorMessage='无效代码';
});
Navigator.of(context.pop();
smsOTPDialog(上下文)。然后((值){
打印(“登录”);
});
打破
违约:
设置状态(){
errorMessage=error.message;
});
打破
}
}
用户界面如下所示
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,
)
],
),
),
);
}
Widget构建(BuildContext){
返回脚手架(
appBar:appBar(
标题:文本(“电话认证”),
),
正文:中心(
子栏(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
填充(
填充:边缘设置。全部(10),
子项:文本字段(
装饰:输入装饰(
hintText:“输入电话号码,例如+910000000000”),
onChanged:(值){
this.phoneNo=值;
},
),
),
(错误消息!=''”
?文本(
错误消息,
样式:TextStyle(颜色:Colors.red),
)
:Container()),
大小盒子(
身高:10,
),
升起按钮(
onPressed:(){
验证电话();
},
子项:文本('Verify'),
textColor:Colors.white,
标高:7,
颜色:颜色,蓝色,
)
],
),
),
);
}
问题是firebase首次使用设备中的电话号码对新用户进行了身份验证,但在重新打开应用程序时,firebase再次要求提供电话号码。此外,在提供电话号码时,firebase没有发送OTP,因为用户已通过身份验证。
3.我将非常感谢你的帮助