Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #76

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/home/profile/account/account_settings.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'package:thc/home/profile/account/account_field.dart';
import 'package:thc/home/profile/account/change_password.dart';
import 'package:thc/home/profile/account/close_account.dart';
import 'package:thc/home/profile/account/delete_account.dart';
import 'package:thc/home/profile/profile.dart';
import 'package:thc/the_good_stuff.dart';

class AccountSettings extends StatefulWidget {
const AccountSettings({super.key});


@override
State<AccountSettings> createState() => _AccountSettingsState();
}
Expand Down Expand Up @@ -71,19 +73,19 @@ class _AccountSettingsState extends State<AccountSettings> {
]),
1 => ListTile(
leading: const Icon(Icons.lock_outline),
title: const Text('change password'),
title: const Text('Change Password'),
onTap: () => navigator.push(const ChangePasswordScreen()),
),
2 => ListTile(
leading: const Icon(Icons.logout),
title: const Text('sign out'),
title: const Text('Sign Out'),
onTap: () async {
final signOut = await navigator.showDialog(
const Dialog.confirm(
titleText: 'sign out',
titleText: 'Sign Out',
bodyText: 'Are you sure you want to sign out?\n'
"You'll need to enter your email & password to sign back in.",
actionText: ('back', 'sign out'),
actionText: ('Back', 'Sign Out'),
actionsAlignment: MainAxisAlignment.spaceEvenly,
),
);
Expand All @@ -93,8 +95,11 @@ class _AccountSettingsState extends State<AccountSettings> {
),
_ => ListTile(
leading: const Icon(Icons.person_off_outlined),
title: const Text('close account'),
onTap: () => navigator.showDialog(const CloseAccount()),
title: const Text('Close Account'),
onTap: () => showDialog(
context: context,
builder: (context) => const DeleteAccountDialog(),
),
Comment on lines -96 to +102
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dang, looks like with this change, we now have delete_account.dart and close_account.dart which are set up to do the exact same thing.

),
},
),
Expand Down
99 changes: 99 additions & 0 deletions lib/home/profile/account/delete_account.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class DeleteAccountDialog extends StatelessWidget {
const DeleteAccountDialog({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Close Account'),
content: const Text('Are you sure you want to permanently delete your account? This action cannot be undone.'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
await deleteAccount(context);
Navigator.of(context).pop();
},
child: const Text('Delete Account'),
),
],
);
}

Future<void> deleteAccount(BuildContext context) async {
try {
final user = FirebaseAuth.instance.currentUser;

if (user != null) {
// Prompt the user for their password
final password = await _showPasswordDialog(context);

if (password != null) {
// Re-authenticate the user
final credential = EmailAuthProvider.credential(
email: user.email!,
password: password,
);

await user.reauthenticateWithCredential(credential);

// Delete user data from Firestore
await FirebaseFirestore.instance.collection('users').doc(user.uid).delete();

// Delete the user's account from Firebase Auth
await user.delete();

// Log out the user
await FirebaseAuth.instance.signOut();

ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Account successfully deleted')),
);

// Optionally navigate to a login or home screen
Navigator.of(context).popUntil((route) => route.isFirst);
}
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error deleting account: $e')),
);
}
}

Future<String?> _showPasswordDialog(BuildContext context) async {
String? password;

await showDialog<String>(
context: context,
builder: (BuildContext context) {
final controller = TextEditingController();
return AlertDialog(
title: const Text('Confirm Password'),
content: TextField(
controller: controller,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
),
actions: <Widget>[
TextButton(
onPressed: () {
password = controller.text;
Navigator.of(context).pop(password);
},
child: const Text('OK'),
),
],
);
},
);

return password;
}
}