在我的应用程序中,有一个登录屏幕、注册屏幕和showProfile屏幕。我正在用电子邮件和密码注册用户。在注册屏幕中,有包含图像、名字、姓氏的字段;电话号码。我可以通过这样做添加用户显示名称,图像URL到特定用户-
UserCredential user = await _auth.createUserWithEmailAndPassword(email: _email, password: _password);
var newUser = user.user;
if (newUser != null) {
Reference imgReference = firebaseStorage.ref().child('UserImages/$_email');
UploadTask uploadTask = imgReference.putFile(imgFile);
TaskSnapshot taskSnapshot = await uploadTask;
String url = await taskSnapshot.ref.getDownloadURL();
if (url != null) {
setState(() {
imgUrl = url;
});
}
await newUser.updatePhotoURL(imgUrl);
await newUser.updateDisplayName(_firstName + ' ' + _lastName);
await newUser.reload();
}
现在我想在注册过程中向用户添加带有OTP验证的电话号码。但如果不创建另一个电话号码为的用户帐户,我就无法对电话号码做同样的事情
因此,在成功登录后,在我的showProfile屏幕中,我可以通过user.phoneNumber将用户电话号码解析到屏幕上,就像我可以为user.displayName&用户.photoURL而不会遇到任何问题。
有没有办法将电话号码添加到注册的电子邮件帐户我搜索了一下,但找不到任何开头的东西。
使用Firebase身份验证验证电话号码视为登录该提供商。因此,您不需要在现有登录中添加电话号码,而是将用户与另一个提供商登录,然后将该新提供商链接到现有用户配置文件。
这些步骤在很大程度上类似于使用电话号码登录,但您不是使用电话凭据呼叫signInWithCredential(credential)
,而是在当前用户上呼叫linkWithCredential(credential)
,如帐户链接文档中所示。
上面是安卓文档的链接,因为我觉得这些文档最清楚地记录了这个过程,而且这个过程在各个平台上都是相似的。有关FlutterFire等同物,请参阅有关电话身份验证和帐户链接的文档。
所以经过长时间的搜索,找到&尝试解决方案,我终于能够解决我的问题。在已注册的电子邮件通行证帐户中添加了一个使用firebase OTP验证的电话号码。谢天谢地,这个答案对我来说非常好。这是代码:
final _auth = FirebaseAuth.instance;
final userInfo = _auth.currentUser;
Future<void> phoneVerification() async {
await _auth.verifyPhoneNumber(
phoneNumber: _mobile,
verificationCompleted: (PhoneAuthCredential phoneAuthCredential) async {
/// This is the main important line to link
await userInfo.linkWithCredential(phoneAuthCredential);
// Not nessecary
/* User? refreshedUser = await refreshUser(userInfo);
if (refreshedUser != null) {
setState(() {
userInfo = refreshedUser;
});
} */
},
verificationFailed: (FirebaseAuthException e) {
// Handle error
print(e);
},
codeSent: (String verificationId, int? resendToken) async {
// write the code to handle the OTP code sent for manual verification in case autoverify doesn't work
},
codeAutoRetrievalTimeout: (String verificationId) {
// Do Something for SMS request timeout
},
);
}