【发布时间】:2021-06-26 09:58:09
【问题描述】:
我已经在 Flutter 中使用 Firebase 实现了电子邮件身份验证。我想从用户那里获取电话号码并通过发送 OTP 来验证它以确保它属于他们,然后将他们的电话号码与他们之前使用电子邮件创建的帐户相关联。
为此,我做了以下工作:
verifyPhoneNumber() async {
await auth.verifyPhoneNumber(
phoneNumber: '+92${widget.phoneNumber}',
verificationCompleted: (PhoneAuthCredential credential) async {
await auth.currentUser.updatePhoneNumber(credential);
// either this occurs or the user needs to manually enter the SMS code
},
verificationFailed: (FirebaseAuthException e) {
print(e.message);
},
codeSent: (String verificationId, int resendToken) {
setState(() {
_otpcode = verificationId;
});
},
codeAutoRetrievalTimeout: (String verificationId) {
setState(() {
_otpcode = verificationId;
});
},
);
}
以上代码将 OTP 发送到号码。现在,当用户将接收到的代码输入到 TextField 中时,我想验证两者是否匹配,以便我可以继续 updatePhoneNumber。
我不确定如何实现验证部分。我有以下代码,不知道如何输出错误(如果有)或确认验证确实发生了。
verifyPhoneCode(otp, code) async {
final AuthCredential credential =
PhoneAuthProvider.credential(verificationId: otp, smsCode: code);
await auth.currentUser.updatePhoneNumber(credential);
}
【问题讨论】:
标签: firebase flutter firebase-authentication