@@ -3,13 +3,20 @@ import 'dart:async';
33import 'package:firebase_auth/firebase_auth.dart' ;
44import 'package:flutter/cupertino.dart' ;
55import 'package:flutter/foundation.dart' show kIsWeb;
6+ import 'package:provider/provider.dart' ;
7+
8+ class FirebasePhoneAuthController extends ChangeNotifier {
9+ static FirebasePhoneAuthController of (
10+ BuildContext context, {
11+ bool listen = true ,
12+ }) =>
13+ Provider .of <FirebasePhoneAuthController >(context, listen: listen);
614
7- class FirebasePhoneAuthService extends ChangeNotifier {
815 /// {@macro timeOutDuration}
916 static const kTimeOutDuration = Duration (seconds: 60 );
1017
11- /// Firebase Auth instance using the default [FirebaseApp] .
12- FirebaseAuth ? _auth = FirebaseAuth .instance;
18+ /// Firebase auth instance using the default [FirebaseApp] .
19+ static final FirebaseAuth _auth = FirebaseAuth .instance;
1320
1421 /// Web confirmation result for OTP.
1522 ConfirmationResult ? _webConfirmationResult;
@@ -116,54 +123,47 @@ class FirebasePhoneAuthService extends ChangeNotifier {
116123 /// [_onLoginFailed] is called with [FirebaseAuthException]
117124 /// object to handle the error.
118125 Future <bool > sendOTP () async {
119- // this.clear();
120- _auth ?? = FirebaseAuth .instance;
121-
122- this .codeSent = false ;
123- await Future .delayed (Duration .zero, () {
124- notifyListeners ();
125- });
126+ codeSent = false ;
127+ await Future .delayed (Duration .zero, notifyListeners);
126128
127- verificationCompleted (AuthCredential authCredential) async {
129+ verificationCompletedCallback (AuthCredential authCredential) async {
128130 await _loginUser (authCredential: authCredential, autoVerified: true );
129131 }
130132
131- verificationFailed (FirebaseAuthException authException) {
133+ verificationFailedCallback (FirebaseAuthException authException) {
132134 if (_onLoginFailed != null ) _onLoginFailed !(authException);
133135 }
134136
135- codeSent (
137+ codeSentCallback (
136138 String verificationId, [
137139 int ? forceResendingToken,
138140 ]) async {
139141 _verificationId = verificationId;
140142 _forceResendingToken = forceResendingToken;
141- this . codeSent = true ;
143+ codeSent = true ;
142144 notifyListeners ();
143145 _setTimer ();
144146 }
145147
146- codeAutoRetrievalTimeout (String verificationId) {
148+ codeAutoRetrievalTimeoutCallback (String verificationId) {
147149 _verificationId = verificationId;
148150 }
149151
150152 try {
151- _auth ?? = FirebaseAuth .instance;
152-
153153 if (kIsWeb) {
154- _webConfirmationResult = await _auth! .signInWithPhoneNumber (
154+ _webConfirmationResult = await _auth.signInWithPhoneNumber (
155155 _phoneNumber! ,
156156 _recaptchaVerifierForWeb,
157157 );
158- this . codeSent = true ;
158+ codeSent = true ;
159159 _setTimer ();
160160 } else {
161- await _auth! .verifyPhoneNumber (
161+ await _auth.verifyPhoneNumber (
162162 phoneNumber: _phoneNumber! ,
163- verificationCompleted: verificationCompleted ,
164- verificationFailed: verificationFailed ,
165- codeSent: codeSent ,
166- codeAutoRetrievalTimeout: codeAutoRetrievalTimeout ,
163+ verificationCompleted: verificationCompletedCallback ,
164+ verificationFailed: verificationFailedCallback ,
165+ codeSent: codeSentCallback ,
166+ codeAutoRetrievalTimeout: codeAutoRetrievalTimeoutCallback ,
167167 timeout: _timeoutDuration,
168168 forceResendingToken: _forceResendingToken,
169169 );
@@ -204,8 +204,7 @@ class FirebasePhoneAuthService extends ChangeNotifier {
204204
205205 // Not on web.
206206 try {
207- _auth ?? = FirebaseAuth .instance;
208- final authResult = await _auth! .signInWithCredential (authCredential! );
207+ final authResult = await _auth.signInWithCredential (authCredential! );
209208 if (_onLoginSuccess != null ) _onLoginSuccess !(authResult, autoVerified);
210209 return true ;
211210 } on FirebaseAuthException catch (e) {
@@ -227,9 +226,7 @@ class FirebasePhoneAuthService extends ChangeNotifier {
227226
228227 /// {@macro signOut}
229228 Future <void > signOut () async {
230- // this.clear();
231- _auth ?? = FirebaseAuth .instance;
232- await _auth! .signOut ();
229+ await _auth.signOut ();
233230 notifyListeners ();
234231 }
235232
@@ -240,7 +237,6 @@ class FirebasePhoneAuthService extends ChangeNotifier {
240237 _recaptchaVerifierForWeb = null ;
241238 }
242239 codeSent = false ;
243- _auth = null ;
244240 _webConfirmationResult = null ;
245241 _onLoginSuccess = null ;
246242 _onLoginFailed = null ;
0 commit comments