Skip to content

Commit 518a6c7

Browse files
authored
feat(ui_auth): add more account actions (#172)
- AccountDeletedAction - DisplayNameChangedAction
1 parent cbe75ca commit 518a6c7

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

packages/firebase_ui_auth/lib/src/actions.dart

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:firebase_auth/firebase_auth.dart';
56
import 'package:flutter/material.dart';
67
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
78

@@ -15,6 +16,8 @@ import 'package:firebase_ui_auth/firebase_ui_auth.dart';
1516
/// - [SMSCodeRequestedAction]
1617
/// - [EmailVerifiedAction]
1718
/// - [ForgotPasswordAction]
19+
/// = [AccountDeletedAction]
20+
/// - [DisplayNameChangedAction]
1821
abstract class FirebaseUIAction {
1922
/// Looks up an instance of an action of the type [T] provided
2023
/// via [FirebaseUIActions].
@@ -84,6 +87,32 @@ class AuthCancelledAction extends FirebaseUIAction {
8487
AuthCancelledAction(this.callback);
8588
}
8689

90+
/// {@template ui.auth.actions.account_deleted}
91+
/// An action that is being called when user has deleted their account.
92+
/// {@endtemplate}
93+
class AccountDeletedAction extends FirebaseUIAction {
94+
/// A callback that is being called when user has deleted their account.
95+
final void Function(BuildContext context, User user) callback;
96+
97+
/// {@macro ui.auth.actions.account_deleted}
98+
AccountDeletedAction(this.callback);
99+
}
100+
101+
/// {@template ui.auth.actions.display_name_changed}
102+
/// An action that is being called when user has changed their display name.
103+
/// {@endtemplate}
104+
class DisplayNameChangedAction extends FirebaseUIAction {
105+
/// A callback that is being called when user has changed their display name.
106+
final void Function(
107+
BuildContext context,
108+
String? oldName,
109+
String newName,
110+
) callback;
111+
112+
/// {@macro ui.auth.actions.display_name_changed}
113+
DisplayNameChangedAction(this.callback);
114+
}
115+
87116
/// {@template ui.auth.actions.flutter_fire_ui_actions}
88117
/// An inherited widget that provides a list of actions down the widget tree.
89118
/// {@endtemplate}

packages/firebase_ui_auth/lib/src/widgets/delete_account_button.dart

+34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ typedef SignInRequiredCallback = Future<bool> Function();
1515

1616
/// {@template ui.auth.widgets.delete_account_button}
1717
/// A button that triggers the deletion of the user's account.
18+
///
19+
/// If you want to perform an action after the account is deleted, you can
20+
/// use [AccountDeletedAction].
21+
///
22+
/// Example usage:
23+
/// ```dart
24+
/// ProfileScreen(
25+
/// actions: [
26+
/// AccountDeletedAction((context, user) {
27+
/// // Do something after the account is deleted.
28+
/// }),
29+
/// ],
30+
/// );
31+
/// ```
32+
///
33+
/// or
34+
///
35+
/// ```dart
36+
/// FirebaseUIActions(
37+
/// actions: [
38+
/// AccountDeletedAction((context, user) {
39+
/// // Do something after the account is deleted.
40+
/// }),
41+
/// ],
42+
/// // MyCustomScreen should use DeleteAccountButton internally.
43+
/// child: MyCustomScreen(),
44+
/// )
45+
/// ```
1846
/// {@endtemplate}
1947
class DeleteAccountButton extends StatefulWidget {
2048
/// {@macro ui.auth.auth_controller.auth}
@@ -55,7 +83,13 @@ class _DeleteAccountButtonState extends State<DeleteAccountButton> {
5583
});
5684

5785
try {
86+
final user = auth.currentUser!;
5887
await auth.currentUser?.delete();
88+
89+
FirebaseUIAction.ofType<AccountDeletedAction>(context)?.callback(
90+
context,
91+
user,
92+
);
5993
await FirebaseUIAuth.signOut(context: context, auth: auth);
6094
} on FirebaseAuthException catch (err) {
6195
if (err.code == 'requires-recent-login') {

packages/firebase_ui_auth/lib/src/widgets/editable_user_display_name.dart

+21
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@ import 'package:flutter/cupertino.dart';
88
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
99
import 'package:flutter/material.dart';
1010

11+
import '../actions.dart';
1112
import 'internal/subtitle.dart';
1213

1314
/// {@template ui.auth.widgets.editable_user_display_name}
1415
/// A widget that displays user name and allows to edit it.
1516
/// If the user name is not provided by neither of the providers,
1617
/// a text field is being shown. Otherwise, a user name is rendered with the
1718
/// edit icon.
19+
///
20+
/// If you want to perform an action after display name is changed, you can
21+
/// use [DisplayNameChangedAction].
22+
///
23+
/// Example usage:
24+
/// ```dart
25+
/// ProfileScreen(
26+
/// actions: [
27+
/// DisplayNameChangedAction((context, oldName, newName) {
28+
/// // Do something with the new name.
29+
/// }),
30+
/// ],
31+
/// );
32+
/// ```
1833
/// {@endtemplate}
1934
class EditableUserDisplayName extends StatefulWidget {
2035
/// {@macro ui.auth.auth_controller.auth}
@@ -57,6 +72,12 @@ class _EditableUserDisplayNameState extends State<EditableUserDisplayName> {
5772

5873
await auth.currentUser?.updateDisplayName(ctrl.text);
5974
await auth.currentUser?.reload();
75+
76+
FirebaseUIAction.ofType<DisplayNameChangedAction>(context)?.callback(
77+
context,
78+
displayName,
79+
ctrl.text,
80+
);
6081
} finally {
6182
setState(() {
6283
_editing = false;

0 commit comments

Comments
 (0)