Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

import 'package:firebase_auth/firebase_auth.

dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_music1/uis/widgets/shared/snackbarMessaging.dart';
import 'package:google_fonts/google_fonts.dart';

class UpdatePasswordScreen extends StatefulWidget {


const UpdatePasswordScreen({super.key});

@override
State<UpdatePasswordScreen> createState() => _UpdatePasswordScreenState();
}

class _UpdatePasswordScreenState extends State<UpdatePasswordScreen> {


final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _newPasswordController = TextEditingController();
final TextEditingController _currentPasswordController = TextEditingController();
final TextEditingController _repeatPasswordController = TextEditingController();
User? user = FirebaseAuth.instance.currentUser;

Future<void> reAuthenticateAndChangePassword(
{required String email,
required String currentPassword,
required String newPassword}) async {
try {
FirebaseAuth auth = FirebaseAuth.instance;
User? user = auth.currentUser;

if (user != null) {
// Create EmailAuthProvider credential for re-authentication
AuthCredential credential = EmailAuthProvider.credential(email: email,
password: currentPassword);

// Re-authenticate the user with the credential


await user.reauthenticateWithCredential(credential);

// Update the password


await user.updatePassword(newPassword).whenComplete(() => snackBarInfo(
context: context, message: 'Password updated successfully.'));

if (kDebugMode) {
print('Password updated successfully.');
}
}
} on FirebaseAuthException catch (e) {
snackBarInfo(
context: context,
message: 'Failed to re-authenticate and update password:The current
password is invalid ');
debugPrint('Failed to re-authenticate and update password: $e');
// Handle any authentication errors
}
}

@override
void dispose() {
_newPasswordController.dispose();
_repeatPasswordController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Colors.orangeAccent,
leading: IconButton(
icon: Icon(
size:20,
Icons.arrow_back_ios,
color: Colors.brown.shade800,
),
onPressed: () => Navigator.pop(context),
),
title: Text(
'Update Password',
style: GoogleFonts.acme(
fontSize: 20, color: Colors.brown.shade800),
),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _currentPasswordController,
obscureText: true,
decoration: InputDecoration(
labelStyle: Theme.of(context)
.textTheme
.bodyMedium!
.copyWith(color: Colors.grey.shade500),
labelText: 'current password',
filled: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.orangeAccent),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.grey),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your last password.';
}
if (value.length < 6) {
return 'Password must be at least 6 characters long.';
}
return null;
},
),
const SizedBox(height: 16.0),
TextFormField(
controller: _newPasswordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'new password',
labelStyle: Theme.of(context)
.textTheme
.bodyMedium!
.copyWith(color: Colors.grey.shade500),
filled: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.orangeAccent),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Colors.grey.shade500),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a new password.';
}
if (value.length < 6) {
return 'Password must be at least 6 characters long.';
}
return null;
},
),
const SizedBox(height: 16.0),
TextFormField(
controller: _repeatPasswordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'repeat password',
labelStyle: Theme.of(context)
.textTheme
.bodyMedium!
.copyWith(color: Colors.grey.shade500),
filled: true,
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.orangeAccent),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: const BorderSide(color: Colors.grey),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please repeat the new password.';
}
if (value != _newPasswordController.text) {
return 'Passwords do not match.';
}
return null;
},
),
const SizedBox(height: 14.0),
SizedBox(
width: 150,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange.shade400,
foregroundColor: Colors.white,
),
onPressed: () {
if (_formKey.currentState!.validate()) {
String currentPassword = _currentPasswordController.text;
String newPassword = _newPasswordController.text;
try {
reAuthenticateAndChangePassword(
email: user!.email!,
currentPassword: currentPassword,
newPassword: newPassword);
} catch (e) {
snackBarInfo(
context: context,
message: 'Failed to update password: $e');

if (kDebugMode) {
print('Failed to update password: $e');
}
}
}
},
child: const Text('Update'),
),
),
],
),
),
),
),
);
}
}

You might also like