From de3d85f997d2611081e87917da5e327c9bcf48ee Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Tue, 4 Apr 2023 10:31:27 -0500 Subject: [PATCH] cambiar numero de telefono --- .../authentication_repository.dart | 22 ++-- lib/src/controllers/new_phone_controller.dart | 112 +++++++++++++++++- lib/src/screens/city.dart | 2 +- lib/src/screens/new_number.dart | 21 ++-- lib/src/screens/profile.dart | 1 + 5 files changed, 133 insertions(+), 25 deletions(-) diff --git a/lib/src/authentication/authentication_repository.dart b/lib/src/authentication/authentication_repository.dart index 91b2512..d45b713 100644 --- a/lib/src/authentication/authentication_repository.dart +++ b/lib/src/authentication/authentication_repository.dart @@ -52,6 +52,17 @@ class AuthenticationRepository extends GetxController { ); } + Future updatePhoneNumber(String verificationId, String smsCode) async { + try { + PhoneAuthCredential credential = PhoneAuthProvider.credential( + verificationId: verificationId, smsCode: smsCode); + await FirebaseAuth.instance.currentUser!.updatePhoneNumber(credential); + print("Phone number updated successfully"); + } catch (e) { + print("Error updating phone number: $e"); + } + } + Future verifyOTP(String otp) async { var credentials = await _auth.signInWithCredential( PhoneAuthProvider.credential( @@ -132,15 +143,4 @@ class AuthenticationRepository extends GetxController { } return city; } - - Future updatePhoneNumber(String verificationId, String smsCode) async { - try { - PhoneAuthCredential credential = PhoneAuthProvider.credential( - verificationId: verificationId, smsCode: smsCode); - await FirebaseAuth.instance.currentUser!.updatePhoneNumber(credential); - print("Phone number updated successfully"); - } catch (e) { - print("Error updating phone number: $e"); - } - } } diff --git a/lib/src/controllers/new_phone_controller.dart b/lib/src/controllers/new_phone_controller.dart index de5ac72..60e12f6 100644 --- a/lib/src/controllers/new_phone_controller.dart +++ b/lib/src/controllers/new_phone_controller.dart @@ -1,13 +1,117 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:prosappco/src/authentication/authentication_repository.dart'; class NewPhoneController extends GetxController { - static NewPhoneController get instance => Get.find(); + final FirebaseAuth _auth = FirebaseAuth.instance; - final newPhoneNo = TextEditingController(); + final newPhoneNo = TextEditingController(text: ''); + final otpCode = TextEditingController(text: ''); - void phoneAuthentication(String newPhoneNo) { - AuthenticationRepository.instance.phoneAuthentication(newPhoneNo); + Future updatePhoneNumber(newPhoneNo) async { + final currentUser = _auth.currentUser; + + if (currentUser?.phoneNumber == newPhoneNo) { + Get.snackbar( + 'Ya estas registrado', + 'Este es tu numero actual.', + snackPosition: SnackPosition.BOTTOM, + ); + } else { + try { + final PhoneVerificationCompleted verificationCompleted = + (PhoneAuthCredential credential) async { + await currentUser?.updatePhoneNumber(credential); + Get.snackbar( + 'Número de teléfono actualizado', + 'El número de teléfono se ha actualizado correctamente.', + snackPosition: SnackPosition.BOTTOM, + ); + }; + + final PhoneVerificationFailed verificationFailed = + (FirebaseAuthException e) { + Get.snackbar( + 'Ingresa un numero de telefono valido', + 'verifica que el campo tenga todos los caracteres o intentalo de nuevo', + snackPosition: SnackPosition.BOTTOM, + ); + }; + + final PhoneCodeSent codeSent = + (String verificationId, [int? forceResendingToken]) { + Get.defaultDialog( + title: 'Ingrese el código de verificación', + content: Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + children: [ + TextField( + controller: otpCode, + decoration: InputDecoration( + labelText: 'Código de verificación', + ), + ), + ], + ), + ), + actions: [ + TextButton( + onPressed: () { + Get.back(); + }, + child: Text('Cancelar'), + ), + ElevatedButton( + onPressed: () async { + try { + final PhoneAuthCredential credential = + PhoneAuthProvider.credential( + verificationId: verificationId, + smsCode: otpCode.text, + ); + await currentUser?.updatePhoneNumber(credential); + Get.back(); + Get.snackbar( + 'Número de teléfono actualizado', + 'El número de teléfono se ha actualizado correctamente.', + snackPosition: SnackPosition.BOTTOM, + ); + } catch (e) { + Get.snackbar( + 'Numero ya registrado', + 'El numero de telefono ingresado ya se encuentra registrado', + snackPosition: SnackPosition.BOTTOM, + ); + } + }, + child: Text('Actualizar'), + ), + ], + ); + }; + + final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout = + (String verificationId) { + // Aquí puedes hacer algo si se agota el tiempo de espera para ingresar el código de verificación automáticamente. + }; + + await _auth.verifyPhoneNumber( + phoneNumber: newPhoneNo, + verificationCompleted: verificationCompleted, + verificationFailed: verificationFailed, + codeSent: codeSent, + codeAutoRetrievalTimeout: codeAutoRetrievalTimeout, + ); + } catch (e) { + print('Error actualizando el número de teléfono: $e'); + Get.snackbar( + 'Error actualizando el número de teléfono', + 'Ha ocurrido un error al actualizar el número de teléfono: $e', + snackPosition: SnackPosition.BOTTOM, + ); + } + } } } diff --git a/lib/src/screens/city.dart b/lib/src/screens/city.dart index 4e4f9d5..dcbdbe0 100644 --- a/lib/src/screens/city.dart +++ b/lib/src/screens/city.dart @@ -124,7 +124,7 @@ class _CityScreenState extends State { if (filteredCities == null) { return const Center( child: CircularProgressIndicator( - valueColor: AlwaysStoppedAnimation(Colors.red), + valueColor: AlwaysStoppedAnimation(Color(0xFF2BA4EC)), ), ); } diff --git a/lib/src/screens/new_number.dart b/lib/src/screens/new_number.dart index 7bddb27..d3b2572 100644 --- a/lib/src/screens/new_number.dart +++ b/lib/src/screens/new_number.dart @@ -2,6 +2,7 @@ import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:intl_phone_field/intl_phone_field.dart'; +import 'package:prosappco/src/controllers/new_phone_controller.dart'; import 'package:prosappco/src/controllers/phone_auth_controller.dart'; class NewNumberScreen extends StatefulWidget { @@ -24,6 +25,7 @@ Future updatePhoneNumber(String verificationId, String smsCode) async { } class _NewNumberScreenState extends State { + final controller = Get.put(NewPhoneController()); String completePhoneNumber = ''; final _formKey = GlobalKey(); @@ -59,10 +61,10 @@ class _NewNumberScreenState extends State { child: Padding( padding: EdgeInsets.only(bottom: 5), child: IntlPhoneField( - // controller: controller.phoneNo, + controller: controller.newPhoneNo, initialCountryCode: 'CO', - onChanged: (phoneNo) { - completePhoneNumber = phoneNo.completeNumber; + onChanged: (newPhoneNo) { + completePhoneNumber = newPhoneNo.completeNumber; }, decoration: InputDecoration( enabledBorder: OutlineInputBorder( @@ -92,14 +94,15 @@ class _NewNumberScreenState extends State { child: Center( child: ElevatedButton( onPressed: () { + controller.updatePhoneNumber(completePhoneNumber.toString()); + // Get.to(NewNumberScreen()); // updatePhoneNumber(completePhoneNumber, '123456'); - if (_formKey.currentState!.validate()) { - PhoneAuthController.instance.phoneAuthentication( - completePhoneNumber.trim(), - ); - Get.to(NewNumberScreen()); - } + // if (_formKey.currentState!.validate()) { + // // PhoneAuthController.instance.phoneAuthentication( + // // completePhoneNumber.trim(), + // // ); + // } }, child: Text( 'Enviar código', diff --git a/lib/src/screens/profile.dart b/lib/src/screens/profile.dart index b1ee8ce..372f91b 100644 --- a/lib/src/screens/profile.dart +++ b/lib/src/screens/profile.dart @@ -389,6 +389,7 @@ class _ProfileScreenState extends State { prefixIcon: Icon(Icons.near_me), suffixIcon: Icon(Icons.arrow_drop_down), hintText: city == '' ? 'Ciudad' : '$city', + hintStyle: TextStyle(color: Colors.black87), ), ),