Professional Documents
Culture Documents
Updatepassword
Updatepassword
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';
@override
State<UpdatePasswordScreen> createState() => _UpdatePasswordScreenState();
}
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);
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'),
),
),
],
),
),
),
),
);
}
}