在我的應用程式中,有一個登錄螢屏、注冊螢屏和一個 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 & user.photoURL執行此操作而不會遇到任何問題。
有什么辦法可以將電話號碼添加到注冊的電子郵件帳戶中嗎?我搜索了它,但找不到任何可以開始的東西。
uj5u.com熱心網友回復:
使用 Firebase 身份驗證驗證電話號碼被視為使用該提供商登錄。因此,不是將電話號碼添加到現有登錄,您實際上是要通過另一個提供商登錄用戶,然后將該新提供商鏈接到現有用戶組態檔。
這些步驟在很大程度上類似于使用電話號碼登錄,但不是signInWithCredential(credential)使用電話憑據呼叫,而是呼叫linkWithCredential(credential)當前用戶,如帳戶鏈接檔案中所示。
上面是 Android 檔案的鏈接,因為我覺得這些檔案最清楚地記錄了這個程序,而且這個程序跨平臺是相似的。對于 FlutterFire 等效項,請參閱有關電話身份驗證和帳戶鏈接的檔案。
uj5u.com熱心網友回復:
因此,經過長時間的搜索、查找和嘗試解決方案,我終于能夠解決我的問題。添加了使用 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
},
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400054.html
