In my app, there is a login screen, registration screen and a showProfile screen. I am registering user with email and password. In the registration screen there are fields contain Image, first name, last name , & phone no. I can add the user display name, image URL to the specific user by doing this-
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();
}
Now I want to add phone no with OTP verification to the user during registration process. . but I can't able to the the same thing for phone no without creating another user account with phone no.
So that after successful login, in my showProfile screen I can parse user phone no. by user.phoneNumber to the screen just like I can do this for user.displayName & user.photoURL without facing any problem.
Is there any way to add phone no to the registered email account? I searched about it but couldn't find anything to start with.
CodePudding user response:
Verifying a phone number with Firebase Authentication is considered signing in with that provider. So instead of adding a phone number to existing sign-in, you're actually going to sign in the user with another provider and then linking that new provider to the existing user profile.
The steps are largely similar to signing in with a phone number, but instead of calling signInWithCredential(credential) with the phone credentials, you call linkWithCredential(credential) on the current user as shown in the documentation on account linking.
Above are links to the Android docs, as I feel those document this process clearest and the process is similar across platforms. For FlutterFire equivalents, see the docs for phone auth and account linking.
CodePudding user response:
So after a long search, find & try solutions, I finally was able to solve my problem. Added a phone number using firebase OTP verification to a already registered email-pass account. Thankfully this answer worked for me just perfectly fine. Here's the code :-
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
},
);
}
Hope it helps somebody. Thanks.
