Skip to content

feat(ui_auth): add more account actions #172

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

Merged
merged 1 commit into from
Nov 5, 2023
Merged
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
29 changes: 29 additions & 0 deletions packages/firebase_ui_auth/lib/src/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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';
import 'package:flutter/material.dart';
import 'package:firebase_ui_auth/firebase_ui_auth.dart';

Expand All @@ -15,6 +16,8 @@ import 'package:firebase_ui_auth/firebase_ui_auth.dart';
/// - [SMSCodeRequestedAction]
/// - [EmailVerifiedAction]
/// - [ForgotPasswordAction]
/// = [AccountDeletedAction]
/// - [DisplayNameChangedAction]
abstract class FirebaseUIAction {
/// Looks up an instance of an action of the type [T] provided
/// via [FirebaseUIActions].
Expand Down Expand Up @@ -84,6 +87,32 @@ class AuthCancelledAction extends FirebaseUIAction {
AuthCancelledAction(this.callback);
}

/// {@template ui.auth.actions.account_deleted}
/// An action that is being called when user has deleted their account.
/// {@endtemplate}
class AccountDeletedAction extends FirebaseUIAction {
/// A callback that is being called when user has deleted their account.
final void Function(BuildContext context, User user) callback;

/// {@macro ui.auth.actions.account_deleted}
AccountDeletedAction(this.callback);
}

/// {@template ui.auth.actions.display_name_changed}
/// An action that is being called when user has changed their display name.
/// {@endtemplate}
class DisplayNameChangedAction extends FirebaseUIAction {
/// A callback that is being called when user has changed their display name.
final void Function(
BuildContext context,
String? oldName,
String newName,
) callback;

/// {@macro ui.auth.actions.display_name_changed}
DisplayNameChangedAction(this.callback);
}

/// {@template ui.auth.actions.flutter_fire_ui_actions}
/// An inherited widget that provides a list of actions down the widget tree.
/// {@endtemplate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ 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}
Expand Down Expand Up @@ -55,7 +83,13 @@ class _DeleteAccountButtonState extends State<DeleteAccountButton> {
});

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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ import 'package:flutter/cupertino.dart';
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
import 'package:flutter/material.dart';

import '../actions.dart';
import 'internal/subtitle.dart';

/// {@template ui.auth.widgets.editable_user_display_name}
/// A widget that displays user name and allows to edit it.
/// If the user name is not provided by neither of the providers,
/// a text field is being shown. Otherwise, a user name is rendered with the
/// edit icon.
///
/// If you want to perform an action after display name is changed, you can
/// use [DisplayNameChangedAction].
///
/// Example usage:
/// ```dart
/// ProfileScreen(
/// actions: [
/// DisplayNameChangedAction((context, oldName, newName) {
/// // Do something with the new name.
/// }),
/// ],
/// );
/// ```
/// {@endtemplate}
class EditableUserDisplayName extends StatefulWidget {
/// {@macro ui.auth.auth_controller.auth}
Expand Down Expand Up @@ -57,6 +72,12 @@ class _EditableUserDisplayNameState extends State<EditableUserDisplayName> {

await auth.currentUser?.updateDisplayName(ctrl.text);
await auth.currentUser?.reload();

FirebaseUIAction.ofType<DisplayNameChangedAction>(context)?.callback(
context,
displayName,
ctrl.text,
);
} finally {
setState(() {
_editing = false;
Expand Down