From cc55af1e803db20fe3713ab8cfb8ffcc5e56ca84 Mon Sep 17 00:00:00 2001 From: Tomas Alabes Date: Wed, 17 Jan 2024 02:46:56 +0100 Subject: [PATCH] Headless signup user metadata option (#69) * headless signup user metadata option * Improve extraMetadata and metadataFields documentation * Add trailing comma --------- Co-authored-by: dshukertjr --- lib/src/components/supa_email_auth.dart | 33 ++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/src/components/supa_email_auth.dart b/lib/src/components/supa_email_auth.dart index fbc8e59..2cc9d90 100644 --- a/lib/src/components/supa_email_auth.dart +++ b/lib/src/components/supa_email_auth.dart @@ -67,13 +67,18 @@ class SupaEmailAuth extends StatefulWidget { /// Callback for sending the password reset email final void Function()? onPasswordResetEmailSent; - /// Callback for when the auth action threw an excepction + /// Callback for when the auth action threw an exception /// /// If set to `null`, a snack bar with error color will show up. final void Function(Object error)? onError; + /// Set of additional fields to the signup form that will become + /// part of the user_metadata final List? metadataFields; + /// Additional properties for user_metadata on signup + final Map? extraMetadata; + /// {@macro supa_email_auth} const SupaEmailAuth({ Key? key, @@ -83,6 +88,7 @@ class SupaEmailAuth extends StatefulWidget { this.onPasswordResetEmailSent, this.onError, this.metadataFields, + this.extraMetadata, }) : super(key: key); @override @@ -204,11 +210,7 @@ class _SupaEmailAuthState extends State { email: _emailController.text.trim(), password: _passwordController.text.trim(), emailRedirectTo: widget.redirectTo, - data: widget.metadataFields == null - ? null - : _metadataControllers.map( - (metaDataField, controller) => - MapEntry(metaDataField.key, controller.text)), + data: _resolveData(), ); widget.onSignUpComplete.call(response); } @@ -295,4 +297,23 @@ class _SupaEmailAuthState extends State { ), ); } + + /// Resolve the user_metadata that we will send during sign-up + /// + /// In case both MetadataFields and extraMetadata have the same + /// key in their map, the MetadataFields (form fields) win + Map _resolveData() { + var extra = widget.extraMetadata ?? {}; + extra.addAll(_resolveMetadataFieldsData()); + return extra; + } + + /// Resolve the user_metadata coming from the metadataFields + Map _resolveMetadataFieldsData() { + return widget.metadataFields != null + ? _metadataControllers.map( + (metaDataField, controller) => + MapEntry(metaDataField.key, controller.text)) + : {}; + } }