cambiar numero de telefono

This commit is contained in:
Juan Felipe Duarte
2023-04-04 10:31:27 -05:00
parent 50737e03dd
commit de3d85f997
5 changed files with 133 additions and 25 deletions
@@ -52,6 +52,17 @@ class AuthenticationRepository extends GetxController {
); );
} }
Future<void> 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<bool> verifyOTP(String otp) async { Future<bool> verifyOTP(String otp) async {
var credentials = await _auth.signInWithCredential( var credentials = await _auth.signInWithCredential(
PhoneAuthProvider.credential( PhoneAuthProvider.credential(
@@ -132,15 +143,4 @@ class AuthenticationRepository extends GetxController {
} }
return city; return city;
} }
Future<void> 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");
}
}
} }
+108 -4
View File
@@ -1,13 +1,117 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:prosappco/src/authentication/authentication_repository.dart'; import 'package:prosappco/src/authentication/authentication_repository.dart';
class NewPhoneController extends GetxController { 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) { Future<void> updatePhoneNumber(newPhoneNo) async {
AuthenticationRepository.instance.phoneAuthentication(newPhoneNo); 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,
);
}
}
} }
} }
+1 -1
View File
@@ -124,7 +124,7 @@ class _CityScreenState extends State<CityScreen> {
if (filteredCities == null) { if (filteredCities == null) {
return const Center( return const Center(
child: CircularProgressIndicator( child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.red), valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF2BA4EC)),
), ),
); );
} }
+12 -9
View File
@@ -2,6 +2,7 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:intl_phone_field/intl_phone_field.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'; import 'package:prosappco/src/controllers/phone_auth_controller.dart';
class NewNumberScreen extends StatefulWidget { class NewNumberScreen extends StatefulWidget {
@@ -24,6 +25,7 @@ Future<void> updatePhoneNumber(String verificationId, String smsCode) async {
} }
class _NewNumberScreenState extends State<NewNumberScreen> { class _NewNumberScreenState extends State<NewNumberScreen> {
final controller = Get.put(NewPhoneController());
String completePhoneNumber = ''; String completePhoneNumber = '';
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
@@ -59,10 +61,10 @@ class _NewNumberScreenState extends State<NewNumberScreen> {
child: Padding( child: Padding(
padding: EdgeInsets.only(bottom: 5), padding: EdgeInsets.only(bottom: 5),
child: IntlPhoneField( child: IntlPhoneField(
// controller: controller.phoneNo, controller: controller.newPhoneNo,
initialCountryCode: 'CO', initialCountryCode: 'CO',
onChanged: (phoneNo) { onChanged: (newPhoneNo) {
completePhoneNumber = phoneNo.completeNumber; completePhoneNumber = newPhoneNo.completeNumber;
}, },
decoration: InputDecoration( decoration: InputDecoration(
enabledBorder: OutlineInputBorder( enabledBorder: OutlineInputBorder(
@@ -92,14 +94,15 @@ class _NewNumberScreenState extends State<NewNumberScreen> {
child: Center( child: Center(
child: ElevatedButton( child: ElevatedButton(
onPressed: () { onPressed: () {
controller.updatePhoneNumber(completePhoneNumber.toString());
// Get.to(NewNumberScreen());
// updatePhoneNumber(completePhoneNumber, '123456'); // updatePhoneNumber(completePhoneNumber, '123456');
if (_formKey.currentState!.validate()) { // if (_formKey.currentState!.validate()) {
PhoneAuthController.instance.phoneAuthentication( // // PhoneAuthController.instance.phoneAuthentication(
completePhoneNumber.trim(), // // completePhoneNumber.trim(),
); // // );
Get.to(NewNumberScreen()); // }
}
}, },
child: Text( child: Text(
'Enviar código', 'Enviar código',
+1
View File
@@ -389,6 +389,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
prefixIcon: Icon(Icons.near_me), prefixIcon: Icon(Icons.near_me),
suffixIcon: Icon(Icons.arrow_drop_down), suffixIcon: Icon(Icons.arrow_drop_down),
hintText: city == '' ? 'Ciudad' : '$city', hintText: city == '' ? 'Ciudad' : '$city',
hintStyle: TextStyle(color: Colors.black87),
), ),
), ),