Skip to content
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
37 changes: 31 additions & 6 deletions lib/controllers/delete_account_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@ import 'dart:developer';
import 'package:appwrite/appwrite.dart';
import 'package:get/get.dart';
import 'package:resonate/controllers/auth_state_controller.dart';
import 'package:resonate/routes/app_routes.dart';
import 'package:resonate/services/appwrite_service.dart';
import 'package:resonate/utils/constants.dart';

class DeleteAccountController extends GetxController {
RxBool isButtonActive = false.obs;
RxBool isLoading = false.obs;

AuthStateController authStateController = Get.put(AuthStateController());

late final Storage storage;
late final TablesDB tables;

//
//-------------------------------------------------------------------
// PLEASE DO NOT TOUCH THIS CODE WITHOUT PERMISSION -
//-------------------------------------------------------------------
//
late final Account account;

@override
void onInit() {
super.onInit();

storage = AppwriteService.getStorage();
tables = AppwriteService.getTables();
account = AppwriteService.getAccount();
}

Future<void> deleteUserProfilePicture() async {
Expand Down Expand Up @@ -62,4 +60,31 @@ class DeleteAccountController extends GetxController {
log(e.toString());
}
}

/// Permanently removes all user data and blocks the auth account,
/// then redirects to the welcome screen.
Future<void> deleteAccount() async {
try {
isLoading.value = true;

// Delete all associated user data
await deleteUserProfilePicture();
await deleteUsernamesCollectionDocument();
await deleteUsersCollectionDocument();

// Block the auth account so the user can no longer log in.
// The Appwrite client SDK does not expose a hard-delete endpoint;
// updateStatus() permanently blocks the account from any access.
await account.updateStatus();

// Invalidate all active sessions
await account.deleteSessions();

Get.offAllNamed(AppRoutes.welcomeScreen);
} catch (e) {
log(e.toString());
} finally {
isLoading.value = false;
}
}
}
25 changes: 17 additions & 8 deletions lib/views/screens/delete_account_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,24 @@ class DeleteAccountScreen extends StatelessWidget {
disabledForegroundColor: Colors.redAccent.withAlpha(100),
disabledBackgroundColor: Colors.redAccent.withAlpha(50),
),
onPressed: (controller.isButtonActive.value)
? () {
// DO NOT IMPLEMENT THIS WITHOUT PERMISSION
}
onPressed: (controller.isButtonActive.value &&
!controller.isLoading.value)
? () => controller.deleteAccount()
: null,
child: Text(
AppLocalizations.of(context)!.iUnderstandDeleteMyAccount,
style: const TextStyle(fontSize: 16),
),
child: controller.isLoading.value
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
color: Colors.white,
strokeWidth: 2,
),
)
: Text(
AppLocalizations.of(context)!
.iUnderstandDeleteMyAccount,
style: const TextStyle(fontSize: 16),
),
),
),
),
Expand Down
Loading