From d7538f49850a73d83a5a2678df6a8f08a8bf5cd0 Mon Sep 17 00:00:00 2001 From: Enrique Cardona Date: Mon, 13 May 2024 16:03:31 +0200 Subject: [PATCH] FIX Password Reset Flow in SupaEmailAuth (#89) * FIX https://github.com/supabase-community/flutter-auth-ui/issues/88 * Fix use of BuildContext's across async gaps * Fix use of BuildContext's across async gaps in SupaEmailAuth and SupaResetPassword --- lib/src/components/supa_email_auth.dart | 12 ++++++++++++ lib/src/components/supa_reset_password.dart | 3 +++ .../localizations/supa_email_auth_localization.dart | 2 ++ .../supa_reset_password_localization.dart | 2 ++ 4 files changed, 19 insertions(+) diff --git a/lib/src/components/supa_email_auth.dart b/lib/src/components/supa_email_auth.dart index 861fb0d..1a5db8c 100644 --- a/lib/src/components/supa_email_auth.dart +++ b/lib/src/components/supa_email_auth.dart @@ -298,10 +298,22 @@ class _SupaEmailAuthState extends State { redirectTo: widget.redirectTo, ); widget.onPasswordResetEmailSent?.call(); + // FIX use_build_context_synchronously + if (!context.mounted) return; + context.showSnackBar(localization.passwordResetSent); + setState(() { + _forgotPassword = false; + }); } on AuthException catch (error) { widget.onError?.call(error); } catch (error) { widget.onError?.call(error); + } finally { + if (mounted) { + setState(() { + _isLoading = false; + }); + } } }, child: Text(localization.sendPasswordReset), diff --git a/lib/src/components/supa_reset_password.dart b/lib/src/components/supa_reset_password.dart index 21d5a22..ab9f695 100644 --- a/lib/src/components/supa_reset_password.dart +++ b/lib/src/components/supa_reset_password.dart @@ -78,6 +78,9 @@ class _SupaResetPasswordState extends State { ), ); widget.onSuccess.call(response); + // FIX use_build_context_synchronously + if (!context.mounted) return; + context.showSnackBar(localization.passwordResetSent); } on AuthException catch (error) { if (widget.onError == null && context.mounted) { context.showErrorSnackBar(error.message); diff --git a/lib/src/localizations/supa_email_auth_localization.dart b/lib/src/localizations/supa_email_auth_localization.dart index ab5f2eb..a37e442 100644 --- a/lib/src/localizations/supa_email_auth_localization.dart +++ b/lib/src/localizations/supa_email_auth_localization.dart @@ -9,6 +9,7 @@ class SupaEmailAuthLocalization { final String dontHaveAccount; final String haveAccount; final String sendPasswordReset; + final String passwordResetSent; final String backToSignIn; final String unexpectedError; @@ -24,6 +25,7 @@ class SupaEmailAuthLocalization { this.dontHaveAccount = 'Don\'t have an account? Sign up', this.haveAccount = 'Already have an account? Sign in', this.sendPasswordReset = 'Send password reset email', + this.passwordResetSent = 'Password reset email has been sent', this.backToSignIn = 'Back to sign in', this.unexpectedError = 'An unexpected error occurred', }); diff --git a/lib/src/localizations/supa_reset_password_localization.dart b/lib/src/localizations/supa_reset_password_localization.dart index 60c085b..bbd1041 100644 --- a/lib/src/localizations/supa_reset_password_localization.dart +++ b/lib/src/localizations/supa_reset_password_localization.dart @@ -3,6 +3,7 @@ class SupaResetPasswordLocalization { final String passwordLengthError; final String updatePassword; final String unexpectedError; + final String passwordResetSent; const SupaResetPasswordLocalization({ this.enterPassword = 'Enter your password', @@ -10,5 +11,6 @@ class SupaResetPasswordLocalization { 'Please enter a password that is at least 6 characters long', this.updatePassword = 'Update Password', this.unexpectedError = 'An unexpected error occurred', + this.passwordResetSent = 'Password reset email has been sent', }); }