Skip to content

Commit cc55af1

Browse files
Headless signup user metadata option (supabase-community#69)
* headless signup user metadata option * Improve extraMetadata and metadataFields documentation * Add trailing comma --------- Co-authored-by: dshukertjr <[email protected]>
1 parent edd83a2 commit cc55af1

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

lib/src/components/supa_email_auth.dart

+27-6
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,18 @@ class SupaEmailAuth extends StatefulWidget {
6767
/// Callback for sending the password reset email
6868
final void Function()? onPasswordResetEmailSent;
6969

70-
/// Callback for when the auth action threw an excepction
70+
/// Callback for when the auth action threw an exception
7171
///
7272
/// If set to `null`, a snack bar with error color will show up.
7373
final void Function(Object error)? onError;
7474

75+
/// Set of additional fields to the signup form that will become
76+
/// part of the user_metadata
7577
final List<MetaDataField>? metadataFields;
7678

79+
/// Additional properties for user_metadata on signup
80+
final Map<String, dynamic>? extraMetadata;
81+
7782
/// {@macro supa_email_auth}
7883
const SupaEmailAuth({
7984
Key? key,
@@ -83,6 +88,7 @@ class SupaEmailAuth extends StatefulWidget {
8388
this.onPasswordResetEmailSent,
8489
this.onError,
8590
this.metadataFields,
91+
this.extraMetadata,
8692
}) : super(key: key);
8793

8894
@override
@@ -204,11 +210,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
204210
email: _emailController.text.trim(),
205211
password: _passwordController.text.trim(),
206212
emailRedirectTo: widget.redirectTo,
207-
data: widget.metadataFields == null
208-
? null
209-
: _metadataControllers.map<String, dynamic>(
210-
(metaDataField, controller) =>
211-
MapEntry(metaDataField.key, controller.text)),
213+
data: _resolveData(),
212214
);
213215
widget.onSignUpComplete.call(response);
214216
}
@@ -295,4 +297,23 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
295297
),
296298
);
297299
}
300+
301+
/// Resolve the user_metadata that we will send during sign-up
302+
///
303+
/// In case both MetadataFields and extraMetadata have the same
304+
/// key in their map, the MetadataFields (form fields) win
305+
Map<String, dynamic> _resolveData() {
306+
var extra = widget.extraMetadata ?? <String, dynamic>{};
307+
extra.addAll(_resolveMetadataFieldsData());
308+
return extra;
309+
}
310+
311+
/// Resolve the user_metadata coming from the metadataFields
312+
Map<String, dynamic> _resolveMetadataFieldsData() {
313+
return widget.metadataFields != null
314+
? _metadataControllers.map<String, dynamic>(
315+
(metaDataField, controller) =>
316+
MapEntry(metaDataField.key, controller.text))
317+
: <String, dynamic>{};
318+
}
298319
}

0 commit comments

Comments
 (0)