-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 4766 - when logging in, show adequate error messages (#4783)
* fix: 4766 - when logging in, show adequate error messages. New file: * `login_result.dart`: Result of a log in attempt, more subtle than a `bool`. Impacted files: * `app_en.arb`: added 2 labels (network unreachable and server KO) * `app_fr.arb`: added 2 labels (network unreachable and server KO) * `background_task_manager.dart`: minor refactoring * `login_page.dart`: refactored using new class `LoginResult` in order to display more adequate error messages. * `pubspec.lock`: wtf * `smooth_app_data_importer.dart`: minor refactoring * `user_management_provider.dart`: refactored using new class `LoginResult` * Update packages/smooth_app/lib/pages/user_management/login_page.dart * Update packages/smooth_app/lib/pages/user_management/login_page.dart * Update packages/smooth_app/lib/pages/user_management/login_page.dart * Delete packages/smooth_app/lib/helpers/data_importer/smooth_app_data_importer.dart no more data_importer
- Loading branch information
1 parent
122aceb
commit a6faac0
Showing
6 changed files
with
127 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:smooth_app/query/product_query.dart'; | ||
|
||
/// How did the login attempt work? | ||
enum LoginResultType { | ||
successful, | ||
unsuccessful, | ||
serverIssue, | ||
exception, | ||
} | ||
|
||
/// Result of a log in attempt, more subtle than a `bool`. | ||
class LoginResult { | ||
const LoginResult(this.type, {this.user, this.text}); | ||
|
||
final LoginResultType type; | ||
final User? user; | ||
final String? text; | ||
|
||
String getErrorMessage(final AppLocalizations appLocalizations) => | ||
switch (type) { | ||
LoginResultType.successful => 'not supposed to happen', | ||
LoginResultType.unsuccessful => appLocalizations.incorrect_credentials, | ||
LoginResultType.serverIssue => | ||
appLocalizations.login_result_type_server_issue, | ||
LoginResultType.exception => isNoNetworkException(text!) | ||
? appLocalizations.login_result_type_server_unreachable | ||
: text!, | ||
}; | ||
|
||
static bool isNoNetworkException(final String text) => | ||
text == 'Network is unreachable' || | ||
text.startsWith('Failed host lookup: '); | ||
|
||
/// Checks credentials. Returns null if OK, or an error message. | ||
static Future<LoginResult> getLoginResult(final User user) async { | ||
try { | ||
final LoginStatus? loginStatus = await OpenFoodAPIClient.login2( | ||
user, | ||
uriHelper: ProductQuery.uriProductHelper, | ||
); | ||
if (loginStatus == null) { | ||
return const LoginResult(LoginResultType.serverIssue); | ||
} | ||
if (!loginStatus.successful) { | ||
return const LoginResult(LoginResultType.unsuccessful); | ||
} | ||
return LoginResult( | ||
LoginResultType.successful, | ||
user: User( | ||
userId: loginStatus.userId!, | ||
password: user.password, | ||
), | ||
); | ||
} catch (e) { | ||
return LoginResult(LoginResultType.exception, text: e.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters