@@ -8,7 +8,7 @@ class FirebasePhoneAuthController extends ChangeNotifier {
88 Provider .of <FirebasePhoneAuthController >(context, listen: listen);
99
1010 /// {@macro autoRetrievalTimeOutDuration}
11- static const kAutoRetrievalTimeOutDuration = Duration (seconds : 60 );
11+ static const kAutoRetrievalTimeOutDuration = Duration (minutes : 1 );
1212
1313 /// Firebase auth instance using the default [FirebaseApp] .
1414 static final FirebaseAuth _auth = FirebaseAuth .instance;
@@ -31,7 +31,10 @@ class FirebasePhoneAuthController extends ChangeNotifier {
3131 String ? _verificationId;
3232
3333 /// Timer object for SMS auto-retrieval.
34- Timer ? _timer;
34+ Timer ? _otpAutoRetrievalTimer;
35+
36+ /// Timer object for OTP expiration.
37+ Timer ? _otpExpirationTimer;
3538
3639 /// Whether OTP to the given phoneNumber is sent or not.
3740 bool codeSent = false ;
@@ -63,26 +66,54 @@ class FirebasePhoneAuthController extends ChangeNotifier {
6366 required bool signOutOnSuccessfulVerification,
6467 RecaptchaVerifier ? recaptchaVerifierForWeb,
6568 Duration autoRetrievalTimeOutDuration = kAutoRetrievalTimeOutDuration,
69+ Duration otpExpirationDuration = kAutoRetrievalTimeOutDuration,
6670 }) {
6771 _phoneNumber = phoneNumber;
6872 _signOutOnSuccessfulVerification = signOutOnSuccessfulVerification;
6973 _onLoginSuccess = onLoginSuccess;
7074 _onCodeSent = onCodeSent;
7175 _onLoginFailed = onLoginFailed;
7276 _autoRetrievalTimeOutDuration = autoRetrievalTimeOutDuration;
77+ _otpExpirationDuration = otpExpirationDuration;
7378 if (kIsWeb) _recaptchaVerifierForWeb = recaptchaVerifierForWeb;
7479 }
7580
76- /// After a [Duration] of [timerCount] , the library no more waits for SMS auto-retrieval.
77- Duration get timerCount =>
78- Duration (seconds: _autoRetrievalTimeOutDuration.inSeconds - (_timer? .tick ?? 0 ));
81+ /// [otpExpirationTimeLeft] can be used to display a reverse countdown, starting from
82+ /// [_otpExpirationDuration.inSeconds] s till 0, and can show the resend
83+ /// button, to let user request a new OTP.
84+ Duration get otpExpirationTimeLeft {
85+ final otpTickDuration = Duration (
86+ seconds: (_otpExpirationTimer? .tick ?? 0 ),
87+ );
88+ return _otpExpirationDuration - otpTickDuration;
89+ }
90+
91+ /// [autoRetrievalTimeLeft] can be used to display a reverse countdown, starting from
92+ /// [_autoRetrievalTimeOutDuration.inSeconds] s till 0, and can show the
93+ /// the listening for OTP view, and also the time left.
94+ ///
95+ /// After this timer is exhausted, the device no longer tries to auto-fetch
96+ /// the OTP, and requires user to manually enter it.
97+ Duration get autoRetrievalTimeLeft {
98+ final otpTickDuration = Duration (
99+ seconds: (_otpAutoRetrievalTimer? .tick ?? 0 ),
100+ );
101+ return _autoRetrievalTimeOutDuration - otpTickDuration;
102+ }
103+
104+ /// Whether the otp has expired or not.
105+ bool get isOtpExpired => ! (_otpExpirationTimer? .isActive ?? false );
79106
80- /// Whether the timer is active or not.
81- bool get timerIsActive => _timer? .isActive ?? false ;
107+ /// Whether the otp retrieval timer is active or not.
108+ bool get isListeningForOtpAutoRetrieve =>
109+ _otpAutoRetrievalTimer? .isActive ?? false ;
82110
83111 /// {@macro autoRetrievalTimeOutDuration}
84112 static Duration _autoRetrievalTimeOutDuration = kAutoRetrievalTimeOutDuration;
85113
114+ /// {@macro otpExpirationDuration}
115+ static Duration _otpExpirationDuration = kAutoRetrievalTimeOutDuration;
116+
86117 /// Verify the OTP sent to [_phoneNumber] and login user is OTP was correct.
87118 ///
88119 /// Returns true if the [otp] passed was correct and the user was logged in successfully.
@@ -93,9 +124,10 @@ class FirebasePhoneAuthController extends ChangeNotifier {
93124 ///
94125 /// Also, [_onLoginFailed] is called with [FirebaseAuthException]
95126 /// object to handle the error.
96- Future <bool > verifyOTP (String otp) async {
127+ Future <bool > verifyOtp (String otp) async {
97128 if ((! kIsWeb && _verificationId == null ) ||
98129 (kIsWeb && _webConfirmationResult == null )) return false ;
130+
99131 try {
100132 if (kIsWeb) {
101133 final userCredential = await _webConfirmationResult! .confirm (otp);
@@ -148,7 +180,6 @@ class FirebasePhoneAuthController extends ChangeNotifier {
148180 _forceResendingToken = forceResendingToken;
149181 codeSent = true ;
150182 _onCodeSent? .call ();
151- notifyListeners ();
152183 _setTimer ();
153184 }
154185
@@ -187,7 +218,7 @@ class FirebasePhoneAuthController extends ChangeNotifier {
187218 }
188219
189220 /// Called when the otp is verified either automatically (OTP auto fetched)
190- /// or [verifyOTP ] was called with the correct OTP.
221+ /// or [verifyOtp ] was called with the correct OTP.
191222 ///
192223 /// If true is returned that means the user was logged in successfully.
193224 ///
@@ -225,12 +256,28 @@ class FirebasePhoneAuthController extends ChangeNotifier {
225256
226257 /// Set timer after code sent.
227258 void _setTimer () {
228- _timer = Timer .periodic (const Duration (seconds: 1 ), (timer) {
229- if (timer.tick == _autoRetrievalTimeOutDuration.inSeconds) _timer? .cancel ();
230- try {
231- notifyListeners ();
232- } catch (_) {}
233- });
259+ _otpExpirationTimer = Timer .periodic (
260+ const Duration (seconds: 1 ),
261+ (timer) {
262+ if (timer.tick == _otpExpirationDuration.inSeconds) {
263+ _otpExpirationTimer? .cancel ();
264+ }
265+ try {
266+ notifyListeners ();
267+ } catch (_) {}
268+ },
269+ );
270+ _otpAutoRetrievalTimer = Timer .periodic (
271+ const Duration (seconds: 1 ),
272+ (timer) {
273+ if (timer.tick == _autoRetrievalTimeOutDuration.inSeconds) {
274+ _otpAutoRetrievalTimer? .cancel ();
275+ }
276+ try {
277+ notifyListeners ();
278+ } catch (_) {}
279+ },
280+ );
234281 notifyListeners ();
235282 }
236283
@@ -253,10 +300,11 @@ class FirebasePhoneAuthController extends ChangeNotifier {
253300 _onCodeSent = null ;
254301 _signOutOnSuccessfulVerification = false ;
255302 _forceResendingToken = null ;
256- _timer ? .cancel ();
257- _timer = null ;
303+ _otpExpirationTimer ? .cancel ();
304+ _otpExpirationTimer = null ;
258305 _phoneNumber = null ;
259306 _autoRetrievalTimeOutDuration = kAutoRetrievalTimeOutDuration;
307+ _otpExpirationDuration = kAutoRetrievalTimeOutDuration;
260308 _verificationId = null ;
261309 }
262310}
0 commit comments