Skip to content

Commit 72a861f

Browse files
authored
feat: Add callback for internal state of SupaEmailAuth (#99)
1 parent 383dce7 commit 72a861f

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lib/src/components/supa_email_auth.dart

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ class SupaEmailAuth extends StatefulWidget {
7373
/// If set to `null`, a snack bar with error color will show up.
7474
final void Function(Object error)? onError;
7575

76+
/// Callback for toggling between sign in and sign up
77+
final void Function(bool isSigningIn)? onToggleSignIn;
78+
79+
/// Callback for toggling between sign-in/ sign-up and password recovery
80+
final void Function(bool isRecoveringPassword)? onToggleRecoverPassword;
81+
7682
/// Set of additional fields to the signup form that will become
7783
/// part of the user_metadata
7884
final List<MetaDataField>? metadataFields;
@@ -91,6 +97,8 @@ class SupaEmailAuth extends StatefulWidget {
9197
required this.onSignUpComplete,
9298
this.onPasswordResetEmailSent,
9399
this.onError,
100+
this.onToggleSignIn,
101+
this.onToggleRecoverPassword,
94102
this.metadataFields,
95103
this.extraMetadata,
96104
this.localization = const SupaEmailAuthLocalization(),
@@ -109,8 +117,9 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
109117
bool _isLoading = false;
110118

111119
/// The user has pressed forgot password button
112-
bool _forgotPassword = false;
120+
bool _isRecoveringPassword = false;
113121

122+
/// Whether the user is signing in or signing up
114123
bool _isSigningIn = true;
115124

116125
@override
@@ -142,8 +151,9 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
142151
TextFormField(
143152
keyboardType: TextInputType.emailAddress,
144153
autofillHints: const [AutofillHints.email],
145-
textInputAction:
146-
_forgotPassword ? TextInputAction.done : TextInputAction.next,
154+
textInputAction: _isRecoveringPassword
155+
? TextInputAction.done
156+
: TextInputAction.next,
147157
validator: (value) {
148158
if (value == null ||
149159
value.isEmpty ||
@@ -158,7 +168,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
158168
),
159169
controller: _emailController,
160170
),
161-
if (!_forgotPassword) ...[
171+
if (!_isRecoveringPassword) ...[
162172
spacer(16),
163173
TextFormField(
164174
autofillHints: _isSigningIn
@@ -261,8 +271,9 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
261271
TextButton(
262272
onPressed: () {
263273
setState(() {
264-
_forgotPassword = true;
274+
_isRecoveringPassword = true;
265275
});
276+
widget.onToggleRecoverPassword?.call(_isRecoveringPassword);
266277
},
267278
child: Text(localization.forgotPassword),
268279
),
@@ -271,16 +282,18 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
271282
key: const ValueKey('toggleSignInButton'),
272283
onPressed: () {
273284
setState(() {
274-
_forgotPassword = false;
285+
_isRecoveringPassword = false;
275286
_isSigningIn = !_isSigningIn;
276287
});
288+
widget.onToggleSignIn?.call(_isSigningIn);
289+
widget.onToggleRecoverPassword?.call(_isRecoveringPassword);
277290
},
278291
child: Text(_isSigningIn
279292
? localization.dontHaveAccount
280293
: localization.haveAccount),
281294
),
282295
],
283-
if (_isSigningIn && _forgotPassword) ...[
296+
if (_isSigningIn && _isRecoveringPassword) ...[
284297
spacer(16),
285298
ElevatedButton(
286299
onPressed: () async {
@@ -302,7 +315,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
302315
if (!context.mounted) return;
303316
context.showSnackBar(localization.passwordResetSent);
304317
setState(() {
305-
_forgotPassword = false;
318+
_isRecoveringPassword = false;
306319
});
307320
} on AuthException catch (error) {
308321
widget.onError?.call(error);
@@ -322,7 +335,7 @@ class _SupaEmailAuthState extends State<SupaEmailAuth> {
322335
TextButton(
323336
onPressed: () {
324337
setState(() {
325-
_forgotPassword = false;
338+
_isRecoveringPassword = false;
326339
});
327340
},
328341
child: Text(localization.backToSignIn),

0 commit comments

Comments
 (0)