Ad
How To Link Phone With Email/Password Authentication?
I am trying to create a flutter app in which after creating a user with email/ password they are saved in the firebase then the user enters his phone number on which OTP is sent and the user is logged in after verification. my problem is that when both of these steps are completed firebase is creating two separate accounts one with email other with the phone. Please tell me how can I create a Single account with Both Email/Password and Phone. I also want to login with both Email/Password or Phone. Or any other way to create a user with email/password and Phone.
void _verifyPhoneNumber() async {
if (mounted)
setState(() {
_message = '';
});
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) {
_firebaseUser.updatePhoneNumberCredential(phoneAuthCredential);
if (mounted)
setState(() {
_message = 'Received phone auth credential: $phoneAuthCredential';
});
};
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
showToast(authException.message,
gravity: Toast.TOP, duration: Toast.LENGTH_LONG);
if (mounted)
setState(() {
_isLoading = false;
_message =
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
});
};
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
print('Please check your phone for the verification code.');
_verificationId = verificationId;
setState(() {
_isLoading = false;
});
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) =>
new VerifyOtp(_firebaseUser, verificationId)));
};
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
_verificationId = verificationId;
};
await _firebaseAuth.verifyPhoneNumber(
phoneNumber: phoneController.text,
timeout: const Duration(minutes: 2),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
}
Verification*
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: widget.verificationId,
smsCode: otpController.text,
);
await _firebaseAuth.signInWithCredential(credential).then((user) {
}).catchError((error) {
showToast(error.toString(),
gravity: Toast.TOP, duration: Toast.LENGTH_LONG);
});
Ad
Answer
await _firebaseAuth.signInWithCredential(credential).then((user) {
}).catchError((error) {
showToast(error.toString(),
gravity: Toast.TOP, duration: Toast.LENGTH_LONG);
});
Just replace it with
firebaseUser.linkWithCredential(credential).then((user) {
print(user.uid);
}).catchError((error) {
print(error.toString());
});
This is work for me.....
Ad
source: stackoverflow.com
Related Questions
- → How can I query Firebase for an equalTo boolean parameter?
- → How can I access nested data in Firebase with React?
- → Firebase simple blog (confused with security rules)
- → Removing item in Firebase with React, re-render returns item undefined
- → AngularJS Unknown Provider Error (Firebase & AngularFire)
- → How do you pass top level component state down to Routes using react-router?
- → "this" is null in firebase query function in reactjs
- → Angular Module Failed to Load
- → Multiple dex files define Lcom/google/android/gms/internal/zzrx;
- → Joining Firebase tables in React
- → How can I make add firepad to my reactjs project?
- → How to use Cloud Functions for Firebase to prerender pages for SEO?
- → React.js component has null state?
Ad