Skip to content
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

Headless signup user metadata option #69

Merged
merged 3 commits into from
Jan 17, 2024
Merged
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
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;
tomasAlabes marked this conversation as resolved.
Show resolved Hide resolved

/// {@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>{};
}
}
Loading