diff --git a/lib/new_ui/screens/profile_screen/profile_screen.dart b/lib/new_ui/screens/profile_screen/profile_screen.dart index 5b6628ca..b7a3a03e 100644 --- a/lib/new_ui/screens/profile_screen/profile_screen.dart +++ b/lib/new_ui/screens/profile_screen/profile_screen.dart @@ -15,6 +15,7 @@ import 'package:tsec_app/models/student_model/student_model.dart'; import 'package:tsec_app/models/user_model/user_model.dart'; import 'package:tsec_app/new_ui/colors.dart'; import 'package:tsec_app/new_ui/screens/profile_screen/widgets/address_text_field.dart'; +import 'package:tsec_app/new_ui/screens/profile_screen/widgets/change_password_dialog.dart'; import 'package:tsec_app/new_ui/screens/profile_screen/widgets/faculty_field.dart'; import 'package:tsec_app/new_ui/screens/profile_screen/widgets/phone_no_field.dart'; import 'package:tsec_app/new_ui/screens/profile_screen/widgets/profile_dropdown_field.dart'; @@ -788,6 +789,11 @@ class _ProfilePageState extends ConsumerState { ), ), ), + TextButton(onPressed: (){ + showDialog(context: context, builder: (contextOfDialog){ + return ChangePasswordDialog(ctx1: context,); + }); + }, child: Text("Change password", style: TextStyle(color: Colors.blueAccent),)) ], ), ) diff --git a/lib/new_ui/screens/profile_screen/widgets/change_password_dialog.dart b/lib/new_ui/screens/profile_screen/widgets/change_password_dialog.dart new file mode 100644 index 00000000..5cdca62b --- /dev/null +++ b/lib/new_ui/screens/profile_screen/widgets/change_password_dialog.dart @@ -0,0 +1,136 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:riverpod/riverpod.dart'; +import 'package:tsec_app/provider/auth_provider.dart'; + +class ChangePasswordDialog extends ConsumerWidget { + ChangePasswordDialog({super.key, required this.ctx1}); + BuildContext ctx1; + + final TextEditingController oldPasswordController = TextEditingController(); + + final TextEditingController newPasswordController = TextEditingController(); + + final TextEditingController confirmPasswordController = TextEditingController(); + + String? validatePassword(){ + if(newPasswordController.text != confirmPasswordController.text){ + return "Passwords do not match"; + } + return null; + } + + @override + Widget build(BuildContext context, WidgetRef ref) { + final _formKey = GlobalKey(); + return AlertDialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(15), + ), + title: Text('Change Password'), + content: Container( + //padding: const EdgeInsets.all(16), + child: Form( + autovalidateMode: AutovalidateMode.onUserInteraction, + key: _formKey, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + // Text( + // 'Change Password', + // style: Theme.of(context).textTheme.headlineSmall, + // ), + const SizedBox(height: 16), + TextFormField( + style: TextStyle(color: Colors.white), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter some text'; + } + return null; + }, + obscureText: true, + controller: oldPasswordController, + decoration: InputDecoration( + errorStyle: TextStyle(fontSize: 12), + focusColor: Colors.red, + fillColor: Colors.transparent, + border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)), + labelText: 'Old Password', + ), + ), + const SizedBox(height: 16), + TextFormField( + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter some text'; + } + if(value != confirmPasswordController.text){ + return "Passwords do not match"; + } + return null; + }, + style: TextStyle(color: Colors.white), + obscureText: true, + controller: newPasswordController, + decoration: InputDecoration( + errorStyle: TextStyle(fontSize: 12), + focusColor: Colors.red, + fillColor: Colors.transparent, + border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)), + labelText: 'New Password', + errorText: validatePassword() + ), + ), + const SizedBox(height: 16), + TextFormField( + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter some text'; + } + if(value != newPasswordController.text){ + return "Passwords do not match"; + } + return null; + }, + style: TextStyle(color: Colors.white), + obscureText: true, + controller: confirmPasswordController, + decoration: InputDecoration( + errorStyle: TextStyle(fontSize: 12), + focusColor: Colors.red, + fillColor: Colors.transparent, + border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)), + labelText: 'Confirm Password', + errorText: validatePassword(), + ), + + ), + const SizedBox(height: 16), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text('Cancel'), + ), + TextButton( + onPressed: () { + if(_formKey.currentState!.validate()){ + ref.watch(authProvider.notifier).changePassword(oldPasswordController.text, newPasswordController.text, ctx1); + Navigator.of(context).pop(); + } + }, + child: Text('Change'), + ), + ], + ), + ], + ), + ), + ), + ); + } +} diff --git a/lib/provider/auth_provider.dart b/lib/provider/auth_provider.dart index af191e4f..e4e86dc7 100644 --- a/lib/provider/auth_provider.dart +++ b/lib/provider/auth_provider.dart @@ -106,10 +106,14 @@ class AuthProvider extends StateNotifier { return await _authService.fetchUserDetails(user, context); } - void changePassword(String password, BuildContext context) { + void updatePassword(String password, BuildContext context) { _authService.updatePassword(password, context); } + void changePassword(String oldPass, String newPass, BuildContext context) { + _authService.changePassword(oldPass, newPass, context); + } + Future getUserData(WidgetRef ref, BuildContext context) async { //this is being called on both splash and login screen final user = _ref.watch(firebaseAuthProvider).currentUser; diff --git a/lib/screens/login_screen/widgets/custom_dialog_box.dart b/lib/screens/login_screen/widgets/custom_dialog_box.dart index c219148c..566d1fa8 100644 --- a/lib/screens/login_screen/widgets/custom_dialog_box.dart +++ b/lib/screens/login_screen/widgets/custom_dialog_box.dart @@ -151,7 +151,7 @@ class _ChangePasswordDialogState extends ConsumerState { child: ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { - ref.watch(authProvider.notifier).changePassword( + ref.watch(authProvider.notifier).updatePassword( _passwordTextEditingController.text.trim(), context); GoRouter.of(context).go('/main'); diff --git a/lib/services/auth_service.dart b/lib/services/auth_service.dart index 87a611d5..959f1c01 100644 --- a/lib/services/auth_service.dart +++ b/lib/services/auth_service.dart @@ -60,6 +60,20 @@ class AuthService { await firebaseAuth.sendPasswordResetEmail(email: email); } + void changePassword(String oldPass, String newPass, BuildContext context){ + User user = firebaseAuth.currentUser!; + final credential = EmailAuthProvider.credential(email: user.email!, password: oldPass); + user.reauthenticateWithCredential(credential).then((value) { + user.updatePassword(newPass).then((_) { + showSnackBar(context, "Password updated successfully"); + }).catchError((error) { + showSnackBar(context, "An error occurred while updating password"); + }); + }).catchError((error) { + showSnackBar(context, "An error occurred while updating password"); + }); + } + void updatePassword(String password, BuildContext context) async { User user = firebaseAuth.currentUser!; await user.updatePassword(password);