@@ -2,6 +2,7 @@ import "dart:async";
22
33import "package:flutter/material.dart" ;
44import "package:flutter_user/flutter_user.dart" ;
5+ import "package:flutter_user/src/models/auth_error_details.dart" ;
56
67class FlutterUserNavigatorUserstory extends StatefulWidget {
78 const FlutterUserNavigatorUserstory ({
@@ -88,47 +89,45 @@ class _FlutterUserNavigatorUserstoryState
8889 options! .loginTranslations.loginTitle,
8990 style: theme.textTheme.headlineLarge,
9091 );
91- var subtitle = Text (options! .loginTranslations.loginSubtitle ?? "" );
92+ var subtitle = Text (options? .loginTranslations.loginSubtitle ?? "" );
9293
9394 FutureOr <void > onLogin (String email, String password) async {
94- await options! .beforeLogin? .call (email, password);
95+ await options? .beforeLogin? .call (email, password);
9596 if (! mounted) return ;
9697 unawaited (showLoadingIndicator (context));
97- var loginResponse = await userService ! . loginWithEmailAndPassword (
98- email : email,
99- password : password ,
100- );
101-
102- if ( ! loginResponse.loginSuccessful ) {
98+ try {
99+ await userService ? . loginWithEmailAndPassword (
100+ email : email ,
101+ password : password,
102+ );
103+ } on AuthException catch (e ) {
103104 if (! mounted) return ;
104105 Navigator .of (context, rootNavigator: true ).pop ();
105106 if (! context.mounted) return ;
106- await errorScaffoldMessenger (context, loginResponse);
107+ var authErrorDetails = options! .authExceptionFormatter.format (e);
108+ await errorScaffoldMessenger (context, authErrorDetails);
107109 return ;
108110 }
109- await options! .afterLogin? .call ();
110111
111- if (loginResponse.loginSuccessful) {
112- var onboardingUser = await options! .onBoardedUser? .call ();
113- if (! mounted) return ;
114- Navigator .of (context, rootNavigator: true ).pop ();
115- if (options! .useOnboarding && onboardingUser? .onboarded == false ) {
116- await push (
117- Onboarding (
118- onboardingFinished: (results) async {
119- await options! .onOnboardingComplete? .call (results);
120- if (! mounted || ! context.mounted) return ;
121- Navigator .of (context).pop ();
122- await pushReplacement (widget.afterLoginScreen);
123- },
124- ),
125- );
126- } else {
127- if (! context.mounted) {
128- return ;
129- }
130- await pushReplacement (widget.afterLoginScreen);
131- }
112+ await options? .afterLogin? .call ();
113+
114+ var onboardingUser = await options? .onBoardedUser? .call ();
115+ if (! mounted) return ;
116+ Navigator .of (context, rootNavigator: true ).pop ();
117+ if (options! .useOnboarding && onboardingUser? .onboarded == false ) {
118+ await push (
119+ Onboarding (
120+ onboardingFinished: (results) async {
121+ await options? .onOnboardingComplete? .call (results);
122+ if (! mounted || ! context.mounted) return ;
123+ Navigator .of (context).pop ();
124+ await pushReplacement (widget.afterLoginScreen);
125+ },
126+ ),
127+ );
128+ } else {
129+ if (! context.mounted) return ;
130+ await pushReplacement (widget.afterLoginScreen);
132131 }
133132 }
134133
@@ -138,11 +137,11 @@ class _FlutterUserNavigatorUserstoryState
138137 options: options! .loginOptions,
139138 onLogin: onLogin,
140139 onForgotPassword: (email, ctx) async {
141- await options! .onForgotPassword? .call (email, ctx) ??
140+ await options? .onForgotPassword? .call (email, ctx) ??
142141 await push (_forgotPasswordScreen ());
143142 },
144143 onRegister: (email, password, context) async {
145- await options! .onRegister? .call (email, password, context) ??
144+ await options? .onRegister? .call (email, password, context) ??
146145 await push (_registrationScreen ());
147146 },
148147 );
@@ -164,24 +163,27 @@ class _FlutterUserNavigatorUserstoryState
164163 );
165164
166165 FutureOr <void > onRequestForgotPassword (String email) async {
167- if (options! .onRequestForgotPassword != null ) {
166+ if (options? .onRequestForgotPassword != null ) {
168167 await options! .onRequestForgotPassword !(email);
169168 return ;
170169 }
171170 unawaited (showLoadingIndicator (context));
172171
173- var requestPasswordReponse =
174- await userService! .requestChangePassword (email: email);
175-
176- if (! mounted) return ;
177- Navigator .of (context).pop ();
178-
179- if (! requestPasswordReponse.requestSuccesfull) {
172+ try {
173+ var response = await userService! .requestChangePassword (email: email);
174+ if (! mounted) return ;
175+ Navigator .of (context).pop ();
176+ if (response.requestSuccesfull) {
177+ await pushReplacement (_forgotPasswordSuccessScreen ());
178+ } else {
179+ await push (_forgotPasswordUnsuccessfullScreen ());
180+ }
181+ } on AuthException catch (_) {
182+ if (! mounted) return ;
183+ Navigator .of (context).pop ();
180184 await push (_forgotPasswordUnsuccessfullScreen ());
181185 return ;
182186 }
183-
184- await pushReplacement (_forgotPasswordSuccessScreen ());
185187 }
186188
187189 return ForgotPasswordForm (
@@ -196,7 +198,7 @@ class _FlutterUserNavigatorUserstoryState
196198 Widget _forgotPasswordSuccessScreen () => ForgotPasswordSuccess (
197199 translations: options! .forgotPasswordTranslations,
198200 onRequestForgotPassword: () async {
199- await options! .onForgotPasswordSuccess? .call () ??
201+ await options? .onForgotPasswordSuccess? .call () ??
200202 // ignore: use_build_context_synchronously
201203 Navigator .of (context).pop ();
202204 },
@@ -205,7 +207,7 @@ class _FlutterUserNavigatorUserstoryState
205207 Widget _forgotPasswordUnsuccessfullScreen () => ForgotPasswordUnsuccessfull (
206208 translations: forgotPasswordTranslations! ,
207209 onPressed: () async {
208- await options! .onForgotPasswordUnsuccessful? .call () ??
210+ await options? .onForgotPasswordUnsuccessful? .call () ??
209211 // ignore: use_build_context_synchronously
210212 Navigator .of (context).pop ();
211213 },
@@ -215,50 +217,47 @@ class _FlutterUserNavigatorUserstoryState
215217 registrationOptions: registrationOptions! ,
216218 userService: userService! ,
217219 onError: (error) async {
218- if (options! .onRegistrationError != null ) {
219- return options!
220- .onRegistrationError !(error ?? "Something went wrong" );
220+ var errorDetails = options! .authExceptionFormatter.format (error);
221+
222+ if (options? .onRegistrationError != null ) {
223+ return options! .onRegistrationError !(error, errorDetails);
221224 }
222225 await push (
223226 _registrationUnsuccessfullScreen (
224- error ?? "Something went wrong" ,
227+ errorDetails ,
225228 ),
226229 );
227- var isPasswordError = error? .contains ("weak-password" ) ?? false ;
228- var isEmailError = error? .contains ("email-already-in-use" ) ?? false ;
229- if (isPasswordError) {
230- return 1 ;
231- }
232- if (isEmailError) {
233- return 0 ;
234- }
230+
231+ if (error is WeakPasswordError ) return 1 ;
232+
233+ if (error is EmailAlreadyInUseError ) return 0 ;
235234
236235 return null ;
237236 },
238237 afterRegistration: () async {
239- options! .afterRegistration? .call () ??
238+ options? .afterRegistration? .call () ??
240239 await pushReplacement (_registrationSuccessScreen ());
241240 },
242241 );
243242
244243 Widget _registrationSuccessScreen () => RegistrationSuccess (
245244 registrationOptions: registrationOptions! ,
246245 onPressed: () async {
247- await options! .afterRegistrationSuccess? .call () ??
246+ await options? .afterRegistrationSuccess? .call () ??
248247 // ignore: use_build_context_synchronously
249248 Navigator .of (context).pop ();
250249 },
251250 );
252251
253- Widget _registrationUnsuccessfullScreen (String error ) =>
252+ Widget _registrationUnsuccessfullScreen (AuthErrorDetails errorDetails ) =>
254253 RegistrationUnsuccessfull (
255254 registrationOptions: registrationOptions! ,
256255 onPressed: () async {
257256 await options! .afterRegistrationUnsuccessful? .call () ??
258257 // ignore: use_build_context_synchronously
259258 Navigator .of (context).pop ();
260259 },
261- error : error ,
260+ errorDetails : errorDetails ,
262261 );
263262
264263 Future <void > push (Widget screen) async {
0 commit comments