Skip to content

Commit 73a69e9

Browse files
committed
* BREAKING: Renamed flag timeOutDuration to autoRetrievalTimeOutDuration
* BREAKING: Updated verifyOTP function signature to not take a named argument * Added callback onCodeSent and flag signOutOnSuccessfulVerification * Added isSendingCode flag to controller * Optimized code to reduce number of rebuilds * Updated example app * Updated dependencies * Refactored code * Updated version
1 parent c28efcf commit 73a69e9

11 files changed

Lines changed: 180 additions & 112 deletions

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## [1.0.5] - 10/07/2022
2+
3+
* BREAKING: Renamed flag timeOutDuration to autoRetrievalTimeOutDuration
4+
* BREAKING: Updated verifyOTP function signature to not take a named argument
5+
* Added callback onCodeSent and flag signOutOnSuccessfulVerification
6+
* Added isSendingCode flag to controller
7+
* Optimized code to reduce number of rebuilds
8+
* Updated example app
9+
* Updated dependencies
10+
* Refactored code
11+
112
## [1.0.4] - 07/06/2022
213

314
* Updated example app

README.md

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ verified manually be calling `verifyOTP`. True if auto verified and false is ver
105105
or any internal error occurs, callback is triggered exposing `FirebaseAuthException`
106106
which can be used to handle the error.
107107

108+
`onCodeSent` is called when the OTP is successfully sent to the phone number.
109+
108110
```dart
109111
FirebasePhoneAuthHandler(
110112
phoneNumber: "+919876543210",
113+
// If true, the user is signed out before the onLoginSuccess callback is fired when the OTP is verified successfully.
114+
signOutOnSuccessfulVerification: false,
111115
builder: (context, controller) {
112116
return SizedBox.shrink();
113117
},
@@ -248,20 +252,20 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
248252
@override
249253
void initState() {
250254
scrollController = ScrollController();
251-
WidgetsBinding.instance?.addObserver(this);
255+
WidgetsBinding.instance.addObserver(this);
252256
super.initState();
253257
}
254258
255259
@override
256260
void dispose() {
257-
WidgetsBinding.instance?.removeObserver(this);
261+
WidgetsBinding.instance.removeObserver(this);
258262
scrollController.dispose();
259263
super.dispose();
260264
}
261265
262266
@override
263267
void didChangeMetrics() {
264-
final bottomViewInsets = WidgetsBinding.instance!.window.viewInsets.bottom;
268+
final bottomViewInsets = WidgetsBinding.instance.window.viewInsets.bottom;
265269
isKeyboardVisible = bottomViewInsets > 0;
266270
}
267271
@@ -285,6 +289,11 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
285289
return SafeArea(
286290
child: FirebasePhoneAuthHandler(
287291
phoneNumber: widget.phoneNumber,
292+
signOutOnSuccessfulVerification: false,
293+
autoRetrievalTimeOutDuration: const Duration(seconds: 60),
294+
onCodeSent: () {
295+
log(VerifyPhoneNumberScreen.id, msg: 'OTP sent!');
296+
},
288297
onLoginSuccess: (userCredential, autoVerified) async {
289298
log(
290299
VerifyPhoneNumberScreen.id,
@@ -320,24 +329,38 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
320329
actions: [
321330
if (controller.codeSent)
322331
TextButton(
323-
child: Text(
324-
controller.timerIsActive
325-
? '${controller.timerCount.inSeconds}s'
326-
: 'Resend',
327-
style: const TextStyle(color: Colors.blue, fontSize: 18),
328-
),
329332
onPressed: controller.timerIsActive
330333
? null
331334
: () async {
332335
log(VerifyPhoneNumberScreen.id, msg: 'Resend OTP');
333336
await controller.sendOTP();
334337
},
338+
child: Text(
339+
controller.timerIsActive
340+
? '${controller.timerCount.inSeconds}s'
341+
: 'Resend',
342+
style: const TextStyle(color: Colors.blue, fontSize: 18),
343+
),
335344
),
336345
const SizedBox(width: 5),
337346
],
338347
),
339-
body: controller.codeSent
340-
? ListView(
348+
body: controller.isSendingCode
349+
? Column(
350+
mainAxisAlignment: MainAxisAlignment.center,
351+
crossAxisAlignment: CrossAxisAlignment.center,
352+
children: const [
353+
CustomLoader(),
354+
SizedBox(height: 50),
355+
Center(
356+
child: Text(
357+
'Sending OTP',
358+
style: TextStyle(fontSize: 25),
359+
),
360+
),
361+
],
362+
)
363+
: ListView(
341364
padding: const EdgeInsets.all(20),
342365
controller: scrollController,
343366
children: [
@@ -381,30 +404,15 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
381404
if (hasFocus) await _scrollToBottomOnKeyboardOpen();
382405
},
383406
onSubmit: (enteredOTP) async {
384-
final isValidOTP = await controller.verifyOTP(
385-
otp: enteredOTP,
386-
);
407+
final isValidOTP =
408+
await controller.verifyOTP(enteredOTP);
387409
// Incorrect OTP
388410
if (!isValidOTP) {
389411
showSnackBar('The entered OTP is invalid!');
390412
}
391413
},
392414
),
393415
],
394-
)
395-
: Column(
396-
mainAxisAlignment: MainAxisAlignment.center,
397-
crossAxisAlignment: CrossAxisAlignment.center,
398-
children: const [
399-
CustomLoader(),
400-
SizedBox(height: 50),
401-
Center(
402-
child: Text(
403-
'Sending OTP',
404-
style: TextStyle(fontSize: 25),
405-
),
406-
),
407-
],
408416
),
409417
);
410418
},

example/lib/screens/verify_phone_number_screen.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
6666
return SafeArea(
6767
child: FirebasePhoneAuthHandler(
6868
phoneNumber: widget.phoneNumber,
69+
signOutOnSuccessfulVerification: false,
70+
autoRetrievalTimeOutDuration: const Duration(seconds: 60),
71+
onCodeSent: () {
72+
log(VerifyPhoneNumberScreen.id, msg: 'OTP sent!');
73+
},
6974
onLoginSuccess: (userCredential, autoVerified) async {
7075
log(
7176
VerifyPhoneNumberScreen.id,
@@ -117,8 +122,22 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
117122
const SizedBox(width: 5),
118123
],
119124
),
120-
body: controller.codeSent
121-
? ListView(
125+
body: controller.isSendingCode
126+
? Column(
127+
mainAxisAlignment: MainAxisAlignment.center,
128+
crossAxisAlignment: CrossAxisAlignment.center,
129+
children: const [
130+
CustomLoader(),
131+
SizedBox(height: 50),
132+
Center(
133+
child: Text(
134+
'Sending OTP',
135+
style: TextStyle(fontSize: 25),
136+
),
137+
),
138+
],
139+
)
140+
: ListView(
122141
padding: const EdgeInsets.all(20),
123142
controller: scrollController,
124143
children: [
@@ -162,30 +181,15 @@ class _VerifyPhoneNumberScreenState extends State<VerifyPhoneNumberScreen>
162181
if (hasFocus) await _scrollToBottomOnKeyboardOpen();
163182
},
164183
onSubmit: (enteredOTP) async {
165-
final isValidOTP = await controller.verifyOTP(
166-
otp: enteredOTP,
167-
);
184+
final isValidOTP =
185+
await controller.verifyOTP(enteredOTP);
168186
// Incorrect OTP
169187
if (!isValidOTP) {
170188
showSnackBar('The entered OTP is invalid!');
171189
}
172190
},
173191
),
174192
],
175-
)
176-
: Column(
177-
mainAxisAlignment: MainAxisAlignment.center,
178-
crossAxisAlignment: CrossAxisAlignment.center,
179-
children: const [
180-
CustomLoader(),
181-
SizedBox(height: 50),
182-
Center(
183-
child: Text(
184-
'Sending OTP',
185-
style: TextStyle(fontSize: 25),
186-
),
187-
),
188-
],
189193
),
190194
);
191195
},

example/pubspec.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,49 @@ packages:
6363
name: firebase_auth
6464
url: "https://pub.dartlang.org"
6565
source: hosted
66-
version: "3.3.19"
66+
version: "3.4.1"
6767
firebase_auth_platform_interface:
6868
dependency: transitive
6969
description:
7070
name: firebase_auth_platform_interface
7171
url: "https://pub.dartlang.org"
7272
source: hosted
73-
version: "6.2.7"
73+
version: "6.3.1"
7474
firebase_auth_web:
7575
dependency: transitive
7676
description:
7777
name: firebase_auth_web
7878
url: "https://pub.dartlang.org"
7979
source: hosted
80-
version: "3.3.16"
80+
version: "3.3.19"
8181
firebase_core:
8282
dependency: "direct main"
8383
description:
8484
name: firebase_core
8585
url: "https://pub.dartlang.org"
8686
source: hosted
87-
version: "1.17.1"
87+
version: "1.19.1"
8888
firebase_core_platform_interface:
8989
dependency: transitive
9090
description:
9191
name: firebase_core_platform_interface
9292
url: "https://pub.dartlang.org"
9393
source: hosted
94-
version: "4.4.0"
94+
version: "4.4.3"
9595
firebase_core_web:
9696
dependency: transitive
9797
description:
9898
name: firebase_core_web
9999
url: "https://pub.dartlang.org"
100100
source: hosted
101-
version: "1.6.4"
101+
version: "1.6.6"
102102
firebase_phone_auth_handler:
103103
dependency: "direct main"
104104
description:
105105
path: ".."
106106
relative: true
107107
source: path
108-
version: "1.0.4"
108+
version: "1.0.5"
109109
flutter:
110110
dependency: "direct main"
111111
description: flutter
@@ -204,7 +204,7 @@ packages:
204204
name: pinput
205205
url: "https://pub.dartlang.org"
206206
source: hosted
207-
version: "2.2.10"
207+
version: "2.2.11"
208208
plugin_platform_interface:
209209
dependency: transitive
210210
description:

example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ dependencies:
1212
flutter:
1313
sdk: flutter
1414

15-
pinput: ^2.2.10
16-
firebase_core: ^1.17.1
15+
pinput: ^2.2.11
16+
firebase_core: ^1.19.1
1717
easy_container: ^1.0.4
1818
intl_phone_field: ^3.1.0
1919
firebase_phone_auth_handler:

0 commit comments

Comments
 (0)