-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathdelete_account_button.dart
131 lines (116 loc) · 3.82 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
// 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'
show FirebaseAuth, FirebaseAuthException;
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();
/// {@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 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;
/// {@macro ui.shared.widgets.button_variant}
final ButtonVariant variant;
/// {@macro ui.auth.widgets.delete_account_button}
const DeleteAccountButton({
super.key,
this.auth,
this.onSignInRequired,
this.onDeleteFailed,
this.variant = ButtonVariant.filled,
});
@override
// ignore: library_private_types_in_public_api
_DeleteAccountButtonState createState() => _DeleteAccountButtonState();
}
class _DeleteAccountButtonState extends State<DeleteAccountButton> {
FirebaseAuth get auth => widget.auth ?? FirebaseAuth.instance;
bool _isLoading = false;
Future<void> _deleteAccount() async {
setState(() {
_isLoading = true;
});
try {
final user = auth.currentUser!;
await auth.currentUser?.delete();
FirebaseUIAction.ofType<AccountDeletedAction>(context)?.callback(
context,
user,
);
await FirebaseUIAuth.signOut(context: context, auth: auth);
} on 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,
);
}
}