Skip to content

Commit

Permalink
Added password change dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicRaptor committed Aug 12, 2024
1 parent 0c4df9d commit 4f66df5
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/new_ui/screens/profile_screen/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -788,6 +789,11 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
),
),
),
TextButton(onPressed: (){
showDialog(context: context, builder: (contextOfDialog){
return ChangePasswordDialog(ctx1: context,);
});
}, child: Text("Change password", style: TextStyle(color: Colors.blueAccent),))
],
),
)
Expand Down
136 changes: 136 additions & 0 deletions lib/new_ui/screens/profile_screen/widgets/change_password_dialog.dart
Original file line number Diff line number Diff line change
@@ -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<FormState>();
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'),
),
],
),
],
),
),
),
);
}
}
6 changes: 5 additions & 1 deletion lib/provider/auth_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ class AuthProvider extends StateNotifier<bool> {
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;
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/login_screen/widgets/custom_dialog_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class _ChangePasswordDialogState extends ConsumerState<ChangePasswordDialog> {
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');
Expand Down
14 changes: 14 additions & 0 deletions lib/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4f66df5

Please sign in to comment.