agregar correo y contraseña
This commit is contained in:
@@ -154,20 +154,21 @@ class _LoginEmailScreenState extends State<LoginEmailScreen> {
|
|||||||
child: TextFormField(
|
child: TextFormField(
|
||||||
controller: controller.email,
|
controller: controller.email,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
enabledBorder: OutlineInputBorder(
|
enabledBorder: OutlineInputBorder(
|
||||||
borderSide: BorderSide(color: Color(0xFFECECEC)),
|
borderSide: BorderSide(color: Color(0xFFECECEC)),
|
||||||
borderRadius: BorderRadius.all(
|
borderRadius: BorderRadius.all(
|
||||||
Radius.circular(50),
|
Radius.circular(50),
|
||||||
)),
|
)),
|
||||||
focusedBorder: OutlineInputBorder(
|
focusedBorder: OutlineInputBorder(
|
||||||
borderSide: BorderSide(color: Color(0xFFECECEC)),
|
borderSide: BorderSide(color: Color(0xFFECECEC)),
|
||||||
borderRadius: BorderRadius.all(
|
borderRadius: BorderRadius.all(
|
||||||
Radius.circular(50),
|
Radius.circular(50),
|
||||||
)),
|
)),
|
||||||
hintText: 'Hello@gmail.com',
|
hintText: 'Hello@gmail.com',
|
||||||
fillColor: Color.fromARGB(255, 239, 239, 239),
|
fillColor: Color.fromARGB(255, 239, 239, 239),
|
||||||
filled: true,
|
filled: true,
|
||||||
prefixIcon: Icon(Icons.email_outlined)),
|
prefixIcon: Icon(Icons.email_outlined),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Padding(
|
Padding(
|
||||||
|
|||||||
@@ -22,21 +22,25 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
File? imagen_to_upload;
|
File? imagen_to_upload;
|
||||||
|
|
||||||
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
||||||
|
bool _obscureText = true;
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
final controller = Get.put(NameEmailCityController());
|
final controller = Get.put(NameEmailCityController());
|
||||||
final _phoneNumberController = TextEditingController();
|
final _phoneNumberController = TextEditingController();
|
||||||
final _nameController = TextEditingController();
|
final _nameController = TextEditingController();
|
||||||
final _emailController = TextEditingController();
|
final _emailController = TextEditingController();
|
||||||
|
final _passwordController = TextEditingController();
|
||||||
late final FirebaseAuth _auth;
|
late final FirebaseAuth _auth;
|
||||||
final FirebaseStorage storage = FirebaseStorage.instance;
|
final FirebaseStorage storage = FirebaseStorage.instance;
|
||||||
var photoTemp = '';
|
var photoTemp = '';
|
||||||
var _ciudad = '...';
|
var _ciudad = '...';
|
||||||
var _photo = '...';
|
var _photo = '...';
|
||||||
var _estado = '...';
|
var _estado = '...';
|
||||||
|
String? _email = '';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
||||||
_auth = FirebaseAuth.instance;
|
_auth = FirebaseAuth.instance;
|
||||||
|
|
||||||
@@ -53,6 +57,8 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
_emailController.text = currentUser.email!;
|
_emailController.text = currentUser.email!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_email = currentUser?.email;
|
||||||
|
|
||||||
if (_ciudad == '...') {
|
if (_ciudad == '...') {
|
||||||
AuthenticationRepository.instance
|
AuthenticationRepository.instance
|
||||||
.getCity(uid.toString())
|
.getCity(uid.toString())
|
||||||
@@ -147,7 +153,6 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
} else {
|
} else {
|
||||||
final imageData = await ref.getData();
|
final imageData = await ref.getData();
|
||||||
if (imageData != null) {
|
if (imageData != null) {
|
||||||
// final widgetImage = Image.memory(imageData);
|
|
||||||
final widgetImage = GestureDetector(
|
final widgetImage = GestureDetector(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
_showChoiceDialog(context);
|
_showChoiceDialog(context);
|
||||||
@@ -216,19 +221,24 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
|
|
||||||
String newName = _nameController.text.trim();
|
String newName = _nameController.text.trim();
|
||||||
String newEmail = _emailController.text.trim();
|
String newEmail = _emailController.text.trim();
|
||||||
|
String newPassword = _passwordController.text.trim();
|
||||||
|
|
||||||
if (newName.isEmpty) {
|
if (newName.isEmpty) {
|
||||||
// Si el nuevo nombre está vacío, no lo actualizamos y mostramos un mensaje al usuario
|
// Si el nuevo nombre está vacío, no lo actualizamos y mostramos un mensaje al usuario
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
Get.snackbar(
|
||||||
SnackBar(content: Text('Ingrese un nombre válido')),
|
'Nombre Invalido',
|
||||||
|
'Ingrese un nombre válido.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newEmail.isEmpty) {
|
if (newEmail.isEmpty) {
|
||||||
// Si el nuevo nombre está vacío, no lo actualizamos y mostramos un mensaje al usuario
|
// Si el nuevo nombre está vacío, no lo actualizamos y mostramos un mensaje al usuario
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
Get.snackbar(
|
||||||
SnackBar(content: Text('Ingrese un email válido')),
|
'Correo Invalido',
|
||||||
|
'Ingrese un email válido.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -246,57 +256,17 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (currentUser?.email != newEmail) {
|
if (currentUser?.email != newEmail) {
|
||||||
try {
|
if (newPassword.isNotEmpty) {
|
||||||
// Enviar un correo electrónico de verificación al nuevo correo electrónico
|
updateEmailAndPassword(newEmail, newPassword);
|
||||||
await FirebaseAuth.instance.currentUser!.updateEmail(newEmail);
|
} else {
|
||||||
await FirebaseAuth.instance.currentUser!.sendEmailVerification();
|
Get.snackbar(
|
||||||
|
'Contraseña Invalida',
|
||||||
// Esperar a que el usuario haga clic en el enlace de verificación enviado a su correo electrónico
|
'Porfavor ingrese una contraseña.',
|
||||||
await FirebaseAuth.instance.currentUser!.reload();
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
while (!FirebaseAuth.instance.currentUser!.emailVerified) {
|
);
|
||||||
await Future.delayed(const Duration(seconds: 1));
|
|
||||||
await FirebaseAuth.instance.currentUser!.reload();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Actualiza los datos del usuario en tu base de datos
|
|
||||||
await FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
||||||
'email': newEmail,
|
|
||||||
});
|
|
||||||
|
|
||||||
print('Correo electrónico actualizado correctamente');
|
|
||||||
} catch (e) {
|
|
||||||
AuthenticationRepository.instance.logout();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (currentUser?.email != newEmail) {
|
|
||||||
// AuthCredential credential =
|
|
||||||
// EmailAuthProvider.credential(email: newEmail, password: '');
|
|
||||||
// try {
|
|
||||||
// await FirebaseAuth.instance.currentUser!.updateEmail(newEmail);
|
|
||||||
// await FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
||||||
// 'email': newEmail,
|
|
||||||
// });
|
|
||||||
// print('Correo electrónico actualizado correctamente');
|
|
||||||
|
|
||||||
// // Reautentica al usuario con las credenciales proporcionadas
|
|
||||||
// await FirebaseAuth.instance.currentUser!
|
|
||||||
// .reauthenticateWithCredential(credential);
|
|
||||||
|
|
||||||
// // Actualiza el correo electrónico del usuario
|
|
||||||
// await FirebaseAuth.instance.currentUser!.updateEmail(newEmail);
|
|
||||||
|
|
||||||
// // Actualiza los datos del usuario en tu base de datos
|
|
||||||
// await FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
||||||
// 'email': newEmail,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// print('Correo electrónico actualizado correctamente');
|
|
||||||
// } catch (e) {
|
|
||||||
// print('Error al actualizar el correo electrónico: $e');
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (imagen_to_upload == null) {
|
if (imagen_to_upload == null) {
|
||||||
return;
|
return;
|
||||||
@@ -310,6 +280,23 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> updateEmailAndPassword(String email, String password) async {
|
||||||
|
final User? user = FirebaseAuth.instance.currentUser;
|
||||||
|
if (user != null) {
|
||||||
|
try {
|
||||||
|
await user.updateEmail(email);
|
||||||
|
await user.updatePassword(password);
|
||||||
|
} catch (e) {
|
||||||
|
Get.snackbar(
|
||||||
|
'Agregar correo',
|
||||||
|
'Inicie sesión para asegurarnos de que seas tú .',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
AuthenticationRepository.instance.logout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _showChoiceDialog(BuildContext context) async {
|
Future<void> _showChoiceDialog(BuildContext context) async {
|
||||||
return showDialog(
|
return showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -358,7 +345,6 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final User? user = FirebaseAuth.instance.currentUser;
|
final User? user = FirebaseAuth.instance.currentUser;
|
||||||
|
|
||||||
String city = _ciudad.toString();
|
String city = _ciudad.toString();
|
||||||
String state = _estado.toString();
|
String state = _estado.toString();
|
||||||
|
|
||||||
@@ -409,23 +395,53 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
children: [
|
children: [
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _nameController,
|
controller: _nameController,
|
||||||
decoration: InputDecoration(
|
decoration: const InputDecoration(
|
||||||
prefixIcon: Icon(Icons.person_outline),
|
prefixIcon: Icon(Icons.person_outline),
|
||||||
hintText: 'Nombre (Obligatorio)'),
|
hintText: 'Nombre (Obligatorio)'),
|
||||||
),
|
),
|
||||||
SizedBox(
|
const SizedBox(
|
||||||
height:
|
|
||||||
20.0), // Agrega espacio vertical entre los TextFormFields
|
|
||||||
|
|
||||||
TextFormField(
|
|
||||||
controller: _emailController,
|
|
||||||
decoration: InputDecoration(
|
|
||||||
prefixIcon: Icon(Icons.email_outlined),
|
|
||||||
hintText: 'Email (Obligatorio)'),
|
|
||||||
),
|
|
||||||
SizedBox(
|
|
||||||
height:
|
height:
|
||||||
20.0), // Agrega espacio vertical entre los TextFormFields
|
20.0), // Agrega espacio vertical entre los TextFormFields
|
||||||
|
_email != null
|
||||||
|
? TextFormField(
|
||||||
|
readOnly: true,
|
||||||
|
controller: _emailController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
prefixIcon: Icon(Icons.email_outlined),
|
||||||
|
hintText: 'Email (Obligatorio)'),
|
||||||
|
)
|
||||||
|
: TextFormField(
|
||||||
|
controller: _emailController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
prefixIcon: Icon(Icons.email_outlined),
|
||||||
|
hintText: 'Email (Obligatorio)'),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 20.0),
|
||||||
|
_email != null
|
||||||
|
? const SizedBox.shrink()
|
||||||
|
: TextFormField(
|
||||||
|
controller: _passwordController,
|
||||||
|
obscureText: _obscureText,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
prefixIcon: const Icon(Icons.lock_outline),
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
_obscureText
|
||||||
|
? Icons.visibility_off
|
||||||
|
: Icons.visibility,
|
||||||
|
color: Colors.grey,
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
setState(() {
|
||||||
|
_obscureText = !_obscureText;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
hintText: 'Contraseña (Obligatorio)'),
|
||||||
|
),
|
||||||
|
_email != null
|
||||||
|
? const SizedBox.shrink()
|
||||||
|
: const SizedBox(height: 20.0),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
readOnly: true,
|
readOnly: true,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
@@ -445,15 +461,9 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
hintStyle: city == ''
|
hintStyle: city == ''
|
||||||
? TextStyle()
|
? TextStyle()
|
||||||
: TextStyle(color: Colors.black87),
|
: TextStyle(color: Colors.black87),
|
||||||
hintText: city == ''
|
hintText: city == '' ? 'Ciudad' : '$city'),
|
||||||
? 'Profesión (Obligatorio)'
|
|
||||||
: '$city'),
|
|
||||||
// hintText: city == '' ? 'Ciudad' : '$city',
|
|
||||||
),
|
),
|
||||||
|
const SizedBox(height: 20.0),
|
||||||
SizedBox(
|
|
||||||
height:
|
|
||||||
20.0), // Agrega espacio vertical entre los TextFormFields
|
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _phoneNumberController,
|
controller: _phoneNumberController,
|
||||||
readOnly: true,
|
readOnly: true,
|
||||||
@@ -470,10 +480,10 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
|||||||
),
|
),
|
||||||
Container(
|
Container(
|
||||||
alignment: Alignment.bottomCenter,
|
alignment: Alignment.bottomCenter,
|
||||||
margin: EdgeInsets.only(top: 170),
|
margin: EdgeInsets.only(top: 100),
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
updateInfo();
|
await updateInfo();
|
||||||
},
|
},
|
||||||
child: Text(
|
child: Text(
|
||||||
'Guardar',
|
'Guardar',
|
||||||
|
|||||||
Reference in New Issue
Block a user