Skip to content

Commit

Permalink
Headless signup user metadata option (#69)
Browse files Browse the repository at this point in the history
* headless signup user metadata option

* Improve extraMetadata and metadataFields documentation

* Add trailing comma

---------

Co-authored-by: dshukertjr <[email protected]>
  • Loading branch information
tomasAlabes and dshukertjr authored Jan 17, 2024
1 parent edd83a2 commit cc55af1
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lib/src/components/supa_email_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetaDataField>? metadataFields;

/// Additional properties for user_metadata on signup
final Map<String, dynamic>? extraMetadata;

/// {@macro supa_email_auth}
const SupaEmailAuth({
Key? key,
Expand All @@ -83,6 +88,7 @@ class SupaEmailAuth extends StatefulWidget {
this.onPasswordResetEmailSent,
this.onError,
this.metadataFields,
this.extraMetadata,
}) : super(key: key);

@override
Expand Down Expand Up @@ -204,11 +210,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
emailRedirectTo: widget.redirectTo,
data: widget.metadataFields == null
? null
: _metadataControllers.map<String, dynamic>(
(metaDataField, controller) =>
MapEntry(metaDataField.key, controller.text)),
data: _resolveData(),
);
widget.onSignUpComplete.call(response);
}
Expand Down Expand Up @@ -295,4 +297,23 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
),
);
}

/// 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<String, dynamic> _resolveData() {
var extra = widget.extraMetadata ?? <String, dynamic>{};
extra.addAll(_resolveMetadataFieldsData());
return extra;
}

/// Resolve the user_metadata coming from the metadataFields
Map<String, dynamic> _resolveMetadataFieldsData() {
return widget.metadataFields != null
? _metadataControllers.map<String, dynamic>(
(metaDataField, controller) =>
MapEntry(metaDataField.key, controller.text))
: <String, dynamic>{};
}
}

0 comments on commit cc55af1

Please sign in to comment.