@@ -57,22 +57,32 @@ class FirebasePhoneAuthController extends ChangeNotifier {
5757 /// {@macro onLoginFailed}
5858 OnLoginFailed ? _onLoginFailed;
5959
60+ /// {@macro onError}
61+ OnError ? _onError;
62+
63+ /// {@macro linkWithExistingUser}
64+ late bool _linkWithExistingUser;
65+
6066 /// Set callbacks and other data. (only for internal use)
6167 void _setData ({
6268 required String phoneNumber,
6369 required OnLoginSuccess ? onLoginSuccess,
6470 required OnLoginFailed ? onLoginFailed,
71+ required OnError ? onError,
6572 required VoidCallback ? onCodeSent,
6673 required bool signOutOnSuccessfulVerification,
67- RecaptchaVerifier ? recaptchaVerifierForWeb,
68- Duration autoRetrievalTimeOutDuration = kAutoRetrievalTimeOutDuration,
69- Duration otpExpirationDuration = kAutoRetrievalTimeOutDuration,
74+ required RecaptchaVerifier ? recaptchaVerifierForWeb,
75+ required Duration autoRetrievalTimeOutDuration,
76+ required Duration otpExpirationDuration,
77+ required bool linkWithExistingUser,
7078 }) {
7179 _phoneNumber = phoneNumber;
7280 _signOutOnSuccessfulVerification = signOutOnSuccessfulVerification;
7381 _onLoginSuccess = onLoginSuccess;
74- _onCodeSent = onCodeSent;
7582 _onLoginFailed = onLoginFailed;
83+ _onError = onError;
84+ _onCodeSent = onCodeSent;
85+ _linkWithExistingUser = linkWithExistingUser;
7686 _autoRetrievalTimeOutDuration = autoRetrievalTimeOutDuration;
7787 _otpExpirationDuration = otpExpirationDuration;
7888 if (kIsWeb) _recaptchaVerifierForWeb = recaptchaVerifierForWeb;
@@ -149,6 +159,7 @@ class FirebasePhoneAuthController extends ChangeNotifier {
149159 _onLoginFailed? .call (e);
150160 return false ;
151161 } catch (e) {
162+ _onError? .call (e);
152163 return false ;
153164 }
154165 }
@@ -213,6 +224,7 @@ class FirebasePhoneAuthController extends ChangeNotifier {
213224 _onLoginFailed? .call (e);
214225 return false ;
215226 } catch (e) {
227+ _onError? .call (e);
216228 return false ;
217229 }
218230 }
@@ -242,14 +254,24 @@ class FirebasePhoneAuthController extends ChangeNotifier {
242254
243255 // Not on web.
244256 try {
245- final authResult = await _auth.signInWithCredential (authCredential! );
257+ late final UserCredential authResult;
258+
259+ if (_linkWithExistingUser) {
260+ authResult = await _auth.currentUser! .linkWithCredential (
261+ authCredential! ,
262+ );
263+ } else {
264+ authResult = await _auth.signInWithCredential (authCredential! );
265+ }
266+
246267 if (_signOutOnSuccessfulVerification) await signOut ();
247268 _onLoginSuccess? .call (authResult, autoVerified);
248269 return true ;
249270 } on FirebaseAuthException catch (e) {
250271 _onLoginFailed? .call (e);
251272 return false ;
252273 } catch (e) {
274+ _onError? .call (e);
253275 return false ;
254276 }
255277 }
@@ -297,12 +319,14 @@ class FirebasePhoneAuthController extends ChangeNotifier {
297319 _webConfirmationResult = null ;
298320 _onLoginSuccess = null ;
299321 _onLoginFailed = null ;
322+ _onError = null ;
300323 _onCodeSent = null ;
301324 _signOutOnSuccessfulVerification = false ;
302325 _forceResendingToken = null ;
303326 _otpExpirationTimer? .cancel ();
304327 _otpExpirationTimer = null ;
305328 _phoneNumber = null ;
329+ _linkWithExistingUser = false ;
306330 _autoRetrievalTimeOutDuration = kAutoRetrievalTimeOutDuration;
307331 _otpExpirationDuration = kAutoRetrievalTimeOutDuration;
308332 _verificationId = null ;
0 commit comments