Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): mobile logging out randomly #11431

Merged
merged 11 commits into from
Jul 30, 2024
Merged

fix(mobile): mobile logging out randomly #11431

merged 11 commits into from
Jul 30, 2024

Conversation

alextran1502
Copy link
Contributor

@alextran1502 alextran1502 commented Jul 29, 2024

Fixes #2980
Fixes #9745
Fixes #8146

This PR simplifies and fixes the issue of occasionally logging out the user when the .well-know endpoint or the getMyUser endpoint returns a status code other than 503 when it cannot be reached. We should now always login provided we have a local version of the user (one successful login has occurred previously) and the API doesn't return a 401 (Auth token has been cancelled)

@alextran1502 alextran1502 marked this pull request as ready for review July 30, 2024 02:51
@zackpollard
Copy link
Contributor

Approved. But also I wrote a lot of the code so let's get at least one review from the mobile guys 🙂

Copy link
Contributor

@martyfuhry martyfuhry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really follow the logic of how this improves the random logouts, but I'm reviewing on my phone without adequate coffee haha! It looks very good and clean. I would like to see some more refactoring. Perhaps some comments about where specifically in this code the random logouts are fixed?

Thank you, I hope my review helps.

error,
stackTrace,
);
UserAdminResponseDto? userResponse;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable will be re-assigned and checked for null.

stackTrace,
);
UserAdminResponseDto? userResponse;
UserPreferencesResponseDto? userPreferences;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables will be re-assigned and checked for null.

mobile/lib/providers/authentication.provider.dart Outdated Show resolved Hide resolved
]);
userResponse = responses[0] as UserAdminResponseDto;
userPreferences = responses[1] as UserPreferencesResponseDto;
} on ApiException catch (error, stackTrace) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does Future.wait handle exceptions? Will this be thrown in this manner?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any exceptions will be presented in the same way. When the await for the Future.wait resolves or rejects

@@ -19,45 +18,22 @@ class SplashScreenPage extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final apiService = ref.watch(apiServiceProvider);
final serverUrl = Store.tryGet(StoreKey.serverUrl);
final endpoint = Store.tryGet(StoreKey.serverEndpoint);
final accessToken = Store.tryGet(StoreKey.accessToken);
final log = Logger("SplashScreenPage");

void performLoggingIn() async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to put this function somewhere else? I typically don't expect to find lots of business logic in the build function of a widget. Maybe we can add this function to a relevant provider. The ui should really just be watching the state of the notifier and updating accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that, but also this is the current state, we are trying to fix the problem without creating more problems by refactoring stuff too much.

@zackpollard
Copy link
Contributor

I don't really follow the logic of how this improves the random logouts, but I'm reviewing on my phone without adequate coffee haha! It looks very good and clean. I would like to see some more refactoring. Perhaps some comments about where specifically in this code the random logouts are fixed?

Thank you, I hope my review helps.

Cheers for the review. To give a brief idea of the changes.

Firstly, we no longer call to the well-known endpoint on every start, reducing one network call. This also removes a bunch of logic dealing with online or offline mode there.

In general, the code now assumes internet connectivity and will callback to the local stored values if the network calls fail. Previously we did all kinds of checks for if the calls succeed or fail and whether things are in stores and it was just needlessly complicated. So now the main logic pulls the value from the store, then does login and updates the values if those calls succeed, then there is a single check for if the user is set and if it is we move on to the main screen.

Another major change was just handling all exceptions from these calls. We now ONLY logout if we don't have the local values we need, or the server returns 401 (the token is revoked). Anything else we absorb and assume the server will come back eventually, if not the user can logout, for example if the server URL has changed.

Feel free to ask any more questions here or on discord 🙂

@alextran1502 alextran1502 merged commit 17c3e8e into main Jul 30, 2024
22 checks passed
@alextran1502 alextran1502 deleted the fix-mobile-logout branch July 30, 2024 18:15
@krisavi
Copy link

krisavi commented Aug 3, 2024

I am wondering if that change caused #11518.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants