-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathdelete_account_button.dart
172 lines (149 loc) · 5.21 KB
/
delete_account_button.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_auth/firebase_auth.dart' as fba;
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
import 'package:firebase_ui_shared/firebase_ui_shared.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
typedef DeleteFailedCallback = void Function(Exception exception);
typedef SignInRequiredCallback = Future<bool> Function();
typedef ActionsBeforeDeleteCallback = Future<bool> Function();
/// {@template ui.auth.widgets.delete_account_button}
/// A button that triggers the deletion of the user's account.
///
/// If you want to perform an action after the account is deleted, you can
/// use [AccountDeletedAction].
///
/// Example usage:
/// ```dart
/// ProfileScreen(
/// actions: [
/// AccountDeletedAction((context, user) {
/// // Do something after the account is deleted.
/// }),
/// ],
/// );
/// ```
///
/// or
///
/// ```dart
/// FirebaseUIActions(
/// actions: [
/// AccountDeletedAction((context, user) {
/// // Do something after the account is deleted.
/// }),
/// ],
/// // MyCustomScreen should use DeleteAccountButton internally.
/// child: MyCustomScreen(),
/// )
/// ```
/// {@endtemplate}
class DeleteAccountButton extends StatefulWidget {
/// {@macro ui.auth.auth_controller.auth}
final fba.FirebaseAuth? auth;
/// A callback tha is called if the [FirebaseAuth] requires the user to
/// re-authenticate and approve the account deletion. By default,
/// [ReauthenticateDialog] is being shown.
final SignInRequiredCallback? onSignInRequired;
/// A callback that is called if the account deletion fails.
final DeleteFailedCallback? onDeleteFailed;
/// A callback that is called before the account is deleted.
final ActionsBeforeDeleteCallback? actionsBeforeDelete;
/// {@macro ui.shared.widgets.button_variant}
final ButtonVariant variant;
/// {@template ui.auth.widgets.delete_account_button.show_delete_confirmation_dialog}
/// If `true`, the user will be asked to confirm the account deletion.
/// {@endtemplate}
final bool showDeleteConfirmationDialog;
/// {@macro ui.auth.widgets.delete_account_button}
const DeleteAccountButton({
super.key,
this.auth,
this.onSignInRequired,
this.onDeleteFailed,
this.actionsBeforeDelete,
this.variant = ButtonVariant.filled,
this.showDeleteConfirmationDialog = false,
});
@override
// ignore: library_private_types_in_public_api
_DeleteAccountButtonState createState() => _DeleteAccountButtonState();
}
class _DeleteAccountButtonState extends State<DeleteAccountButton> {
fba.FirebaseAuth get auth => widget.auth ?? fba.FirebaseAuth.instance;
bool _isLoading = false;
void Function() pop<T>(T result) => () => Navigator.of(context).pop(result);
Future<void> _deleteAccount() async {
bool? confirmed = !widget.showDeleteConfirmationDialog;
if (!confirmed) {
final l = FirebaseUILocalizations.labelsOf(context);
confirmed = await showCupertinoDialog<bool?>(
context: context,
builder: (context) {
return UniversalAlert(
onConfirm: pop(true),
onCancel: pop(false),
title: l.confirmDeleteAccountAlertTitle,
confirmButtonText: l.confirmDeleteAccountButtonLabel,
cancelButtonText: l.cancelButtonLabel,
message: l.confirmDeleteAccountAlertMessage,
);
},
);
}
if (!(confirmed ?? false)) return;
setState(() {
_isLoading = true;
});
try {
if (widget.actionsBeforeDelete != null) {
final proceed = await widget.actionsBeforeDelete!();
if (!proceed) {
throw Exception('Can not complete actions before delete.');
}
}
final user = auth.currentUser!;
await auth.currentUser?.delete();
FirebaseUIAction.ofType<AccountDeletedAction>(context)?.callback(
context,
user,
);
await FirebaseUIAuth.signOut(context: context, auth: auth);
} on fba.FirebaseAuthException catch (err) {
if (err.code == 'requires-recent-login') {
if (widget.onSignInRequired != null) {
final signedIn = await widget.onSignInRequired!();
if (signedIn) {
await _deleteAccount();
}
}
}
} on Exception catch (e) {
widget.onDeleteFailed?.call(e);
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
final l = FirebaseUILocalizations.labelsOf(context);
final themeData = Theme.of(context);
final colorScheme = themeData.colorScheme;
return LoadingButton(
isLoading: _isLoading,
cupertinoColor: CupertinoColors.destructiveRed,
materialColor: colorScheme.error,
cupertinoIcon: CupertinoIcons.delete,
materialIcon: Icons.delete,
label: l.deleteAccount,
labelColor: colorScheme.onError,
onTap: _deleteAccount,
variant: widget.variant,
);
}
}