Skip to content

Commit 7ffe9ec

Browse files
committed
* Added a boolean linkWithExistingUser to link the new credentials with an existing signed-in user, instead of creating a new one.
* Added onError callback for general purpose errors by the library * Updated example app * Updated version
1 parent e13d61b commit 7ffe9ec

8 files changed

Lines changed: 78 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [1.0.6] - 16/07/2022
2+
3+
* Added a boolean linkWithExistingUser to link the new credentials with an existing signed-in user, instead of creating a new one.
4+
* Added onError callback for general purpose errors by the library
5+
16
## [1.0.5+1] - 11/07/2022
27

38
* Fixed files formatting

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ FirebasePhoneAuthHandler(
112112
phoneNumber: "+919876543210",
113113
// If true, the user is signed out before the onLoginSuccess callback is fired when the OTP is verified successfully.
114114
signOutOnSuccessfulVerification: false,
115+
116+
linkWithExistingUser: false,
115117
builder: (context, controller) {
116118
return SizedBox.shrink();
117119
},
@@ -122,6 +124,7 @@ FirebasePhoneAuthHandler(
122124
onLoginFailed: (authException) {
123125
debugPrint("An error occurred: ${authException.message}");
124126
},
127+
onError: (error) {},
125128
),
126129
```
127130

@@ -289,7 +292,8 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
289292
return SafeArea(
290293
child: FirebasePhoneAuthHandler(
291294
phoneNumber: widget.phoneNumber,
292-
signOutOnSuccessfulVerification: false,
295+
signOutOnSuccessfulVerification: false, // default
296+
linkWithExistingUser: false, // default
293297
autoRetrievalTimeOutDuration: const Duration(seconds: 60),
294298
otpExpirationDuration: const Duration(seconds: 60),
295299
onCodeSent: () {
@@ -321,6 +325,9 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
321325
log(VerifyPhoneNumberScreen.id, error: authException.message);
322326
// handle error further if needed
323327
},
328+
onError: (error) {
329+
showSnackBar('An error occurred!');
330+
},
324331
builder: (context, controller) {
325332
return Scaffold(
326333
appBar: AppBar(

example/lib/screens/verify_phone_number_screen.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
6767
child: FirebasePhoneAuthHandler(
6868
phoneNumber: widget.phoneNumber,
6969
signOutOnSuccessfulVerification: false,
70+
linkWithExistingUser: false,
7071
autoRetrievalTimeOutDuration: const Duration(seconds: 60),
7172
otpExpirationDuration: const Duration(seconds: 60),
7273
onCodeSent: () {
@@ -98,6 +99,9 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
9899
log(VerifyPhoneNumberScreen.id, error: authException.message);
99100
// handle error further if needed
100101
},
102+
onError: (error) {
103+
showSnackBar('An error occurred!');
104+
},
101105
builder: (context, controller) {
102106
return Scaffold(
103107
appBar: AppBar(

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ packages:
105105
path: ".."
106106
relative: true
107107
source: path
108-
version: "1.0.5+1"
108+
version: "1.0.6"
109109
flutter:
110110
dependency: "direct main"
111111
description: flutter

lib/src/auth_controller.dart

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

lib/src/auth_handler.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class FirebasePhoneAuthHandler extends StatefulWidget {
1414
Key? key,
1515
required this.phoneNumber,
1616
required this.builder,
17-
this.onLoginFailed,
1817
this.onLoginSuccess,
18+
this.onLoginFailed,
19+
this.onError,
1920
this.onCodeSent,
2021
this.signOutOnSuccessfulVerification = false,
22+
this.linkWithExistingUser = false,
2123
this.autoRetrievalTimeOutDuration =
2224
FirebasePhoneAuthController.kAutoRetrievalTimeOutDuration,
2325
this.otpExpirationDuration =
@@ -56,6 +58,21 @@ class FirebasePhoneAuthHandler extends StatefulWidget {
5658
/// {@endtemplate}
5759
final VoidCallback? onCodeSent;
5860

61+
/// {@template linkWithExistingUser}
62+
///
63+
/// If true, links the generated credentials to an existing signed in user,
64+
/// and not creating new session.
65+
///
66+
/// Internally, if true, this calls the linkWithCredential method instead of
67+
/// signInWithCredential.
68+
///
69+
/// Make sure a user is signed in already, else an error is thrown.
70+
///
71+
/// Defaults to false
72+
///
73+
/// {@endtemplate}
74+
final bool linkWithExistingUser;
75+
5976
/// {@template onLoginSuccess}
6077
///
6178
/// This callback is triggered when the phone number is verified and the user is
@@ -78,6 +95,15 @@ class FirebasePhoneAuthHandler extends StatefulWidget {
7895
/// {@endtemplate}
7996
final OnLoginFailed? onLoginFailed;
8097

98+
/// {@template onError}
99+
///
100+
/// Called when a general error occurs.
101+
///
102+
/// If the error is a [FirebaseAuthException], then [onLoginFailed] is called.
103+
///
104+
/// {@endtemplate}
105+
final OnError? onError;
106+
81107
/// {@template autoRetrievalTimeOutDuration}
82108
///
83109
/// The maximum amount of time you are willing to wait for SMS
@@ -161,9 +187,11 @@ class _FirebasePhoneAuthHandlerState extends State<FirebasePhoneAuthHandler> {
161187
phoneNumber: widget.phoneNumber,
162188
onLoginSuccess: widget.onLoginSuccess,
163189
onLoginFailed: widget.onLoginFailed,
190+
onError: widget.onError,
164191
autoRetrievalTimeOutDuration: widget.autoRetrievalTimeOutDuration,
165192
otpExpirationDuration: widget.otpExpirationDuration,
166193
onCodeSent: widget.onCodeSent,
194+
linkWithExistingUser: widget.linkWithExistingUser,
167195
signOutOnSuccessfulVerification: widget.signOutOnSuccessfulVerification,
168196
recaptchaVerifierForWeb: captcha,
169197
);

lib/src/type_definitions.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ import 'package:firebase_auth/firebase_auth.dart';
44

55
typedef OnLoginSuccess = FutureOr<void> Function(UserCredential, bool);
66
typedef OnLoginFailed = FutureOr<void> Function(FirebaseAuthException);
7+
typedef OnError = FutureOr<void> Function(Object);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: firebase_phone_auth_handler
22
description: An easy-to-use firebase phone authentication package to easily send and verify OTP's with auto-fetch OTP support via SMS. Supports web out of the box.
3-
version: 1.0.5+1
3+
version: 1.0.6
44
homepage: https://github.com/rithik-dev/firebase_phone_auth_handler
55

66
environment:

0 commit comments

Comments
 (0)