Skip to content

Commit

Permalink
fix: Check if user credential still holds (#3077)
Browse files Browse the repository at this point in the history
* check if user credential still holds

* changes suggested on the pr
  • Loading branch information
AshAman999 authored Oct 1, 2022
1 parent a02243e commit 0400c18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/smooth_app/lib/data_models/user_management_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:smooth_app/database/dao_secured_string.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/services/smooth_services.dart';

class UserManagementProvider with ChangeNotifier {
Expand Down Expand Up @@ -82,6 +83,31 @@ class UserManagementProvider with ChangeNotifier {
notifyListeners();
}

/// Check if the user is still logged in and the credentials are still valid
/// If not, the user is logged out
Future<void> checkUserLoginValidity() async {
try {
if (ProductQuery.isLoggedIn()) {
final User user = ProductQuery.getUser();
final bool checkLogin = await OpenFoodAPIClient.login(
User(
userId: user.userId,
password: user.password,
),
);
if (checkLogin) {
// Credentials are still valid so we just return
return;
} else {
// Credentials are not valid anymore so we log out
await logout();
}
}
} catch (e) {
// We don't want to crash the app if the login check fails
// So we do nothing here
}
}
/* Currently not in use, to be used before contributing to something
/// Checks if the saved credentials are still correct
Future<bool> validateCredentials() async {
Expand Down
1 change: 1 addition & 0 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Future<bool> _init1() async {
daoString: DaoString(_localDatabase),
);
await callbackDispatcher(_localDatabase);
UserManagementProvider().checkUserLoginValidity();

AnalyticsHelper.setCrashReports(_userPreferences.crashReports);
ProductQuery.setCountry(_userPreferences.userCountryCode);
Expand Down

0 comments on commit 0400c18

Please sign in to comment.