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

feat: Add callback for internal state of SupaEmailAuth #99

Merged
merged 1 commit into from
Jun 20, 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
31 changes: 22 additions & 9 deletions lib/src/components/supa_email_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class SupaEmailAuth extends StatefulWidget {
/// If set to `null`, a snack bar with error color will show up.
final void Function(Object error)? onError;

/// Callback for toggling between sign in and sign up
final void Function(bool isSigningIn)? onToggleSignIn;

/// Callback for toggling between sign-in/ sign-up and password recovery
final void Function(bool isRecoveringPassword)? onToggleRecoverPassword;

/// Set of additional fields to the signup form that will become
/// part of the user_metadata
final List<MetaDataField>? metadataFields;
Expand All @@ -91,6 +97,8 @@ class SupaEmailAuth extends StatefulWidget {
required this.onSignUpComplete,
this.onPasswordResetEmailSent,
this.onError,
this.onToggleSignIn,
this.onToggleRecoverPassword,
this.metadataFields,
this.extraMetadata,
this.localization = const SupaEmailAuthLocalization(),
Expand All @@ -109,8 +117,9 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
bool _isLoading = false;

/// The user has pressed forgot password button
bool _forgotPassword = false;
bool _isRecoveringPassword = false;

/// Whether the user is signing in or signing up
bool _isSigningIn = true;

@override
Expand Down Expand Up @@ -142,8 +151,9 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
TextFormField(
keyboardType: TextInputType.emailAddress,
autofillHints: const [AutofillHints.email],
textInputAction:
_forgotPassword ? TextInputAction.done : TextInputAction.next,
textInputAction: _isRecoveringPassword
? TextInputAction.done
: TextInputAction.next,
validator: (value) {
if (value == null ||
value.isEmpty ||
Expand All @@ -158,7 +168,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
),
controller: _emailController,
),
if (!_forgotPassword) ...[
if (!_isRecoveringPassword) ...[
spacer(16),
TextFormField(
autofillHints: _isSigningIn
Expand Down Expand Up @@ -261,8 +271,9 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
TextButton(
onPressed: () {
setState(() {
_forgotPassword = true;
_isRecoveringPassword = true;
});
widget.onToggleRecoverPassword?.call(_isRecoveringPassword);
},
child: Text(localization.forgotPassword),
),
Expand All @@ -271,16 +282,18 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
key: const ValueKey('toggleSignInButton'),
onPressed: () {
setState(() {
_forgotPassword = false;
_isRecoveringPassword = false;
_isSigningIn = !_isSigningIn;
});
widget.onToggleSignIn?.call(_isSigningIn);
widget.onToggleRecoverPassword?.call(_isRecoveringPassword);
},
child: Text(_isSigningIn
? localization.dontHaveAccount
: localization.haveAccount),
),
],
if (_isSigningIn && _forgotPassword) ...[
if (_isSigningIn && _isRecoveringPassword) ...[
spacer(16),
ElevatedButton(
onPressed: () async {
Expand All @@ -302,7 +315,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
if (!context.mounted) return;
context.showSnackBar(localization.passwordResetSent);
setState(() {
_forgotPassword = false;
_isRecoveringPassword = false;
});
} on AuthException catch (error) {
widget.onError?.call(error);
Expand All @@ -322,7 +335,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
TextButton(
onPressed: () {
setState(() {
_forgotPassword = false;
_isRecoveringPassword = false;
});
},
child: Text(localization.backToSignIn),
Expand Down
Loading