Closed Prime-Ext closed 2 years ago
class AuthProvider with ChangeNotifier{
FirebaseAuth _auth = FirebaseAuth.instance; late String smsOtp; late String verificationId; String error = ''; UserServices _userServices = UserServices();
FutureverifyPhone(BuildContext context, String number)async { final PhoneVerificationCompleted verificationCompleted = (PhoneAuthCredential credential) async { await _auth.signInWithCredential(credential); };
final PhoneVerificationFailed verificationFailed = (FirebaseAuthException e){ print(e.code); }; final PhoneCodeSent smsOtpSend = (String verId, int resendToken)async { this.verificationId = verId; } as PhoneCodeSent; smsOtpDialog(context,number); try{ _auth.verifyPhoneNumber( phoneNumber: number, verificationCompleted: verificationCompleted, verificationFailed: verificationFailed, codeSent: smsOtpSend, codeAutoRetrievalTimeout: (String verId){ this.verificationId = verId; }, ); }catch(e){ print(e); } } Future<bool?> smsOtpDialog(BuildContext context, String number){ return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Column( children: [ Text('Verification Code'), SizedBox(height: 6,), Text('Enter the 6 didgit Code received by sms', style: TextStyle(color: Colors.grey,fontSize: 10), ), ], ), content: Container( height: 85, child: TextField( textAlign: TextAlign.center, keyboardType: TextInputType.number, maxLength: 6, onChanged: (value) { this.smsOtp = value; }, ), ), actions: [ FlatButton( onPressed: ()async{ (context); try{ PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential( verificationId: verificationId, smsCode: smsOtp, ); final User? user = (await _auth.signInWithCredential(phoneAuthCredential)).user; //create user data in fireStore after successful login _createUser(id: user?.uid,number: user?.phoneNumber ); //navigate to homepage after login if(user!=null){ Navigator.of(context).pop(); //dont want to come back to welcome screen after login Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context)=>HomeScreen(), )); }else{ print('Login Failed'); } }catch (e){ this.error = 'Invalid Code'; print(e.toString()); notifyListeners(); Navigator.of(context).pop(); } }, child: Text('DONE',style: TextStyle(color: Theme.of(context).primaryColor),), ), ], ); }); } void _createUser({String? id, String? number}){ _userServices.createUserData({ 'id':id, 'number':number, });
} }
class AuthProvider with ChangeNotifier{
FirebaseAuth _auth = FirebaseAuth.instance; late String smsOtp; late String verificationId; String error = ''; UserServices _userServices = UserServices();
FutureverifyPhone(BuildContext context, String number)async {
final PhoneVerificationCompleted verificationCompleted =
(PhoneAuthCredential credential) async {
await _auth.signInWithCredential(credential);
};
} }