Handle logged out sessions

This commit is contained in:
Neeraj Gupta 2023-04-04 15:46:18 +05:30
parent 1551842886
commit e140e9dfa7
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
5 changed files with 72 additions and 1 deletions

View file

@ -0,0 +1,3 @@
import 'package:ente_auth/events/event.dart';
class TriggerLogoutEvent extends Event {}

View file

@ -1,10 +1,15 @@
// ignore_for_file: import_of_legacy_library_into_null_safe
import 'dart:async';
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/core/event_bus.dart';
import 'package:ente_auth/ente_theme_data.dart';
import 'package:ente_auth/events/trigger_logout_event.dart';
import "package:ente_auth/l10n/l10n.dart";
import 'package:ente_auth/ui/account/email_entry_page.dart';
import 'package:ente_auth/ui/account/login_page.dart';
import 'package:ente_auth/ui/account/logout_dialog.dart';
import 'package:ente_auth/ui/account/password_entry_page.dart';
import 'package:ente_auth/ui/account/password_reentry_page.dart';
import 'package:ente_auth/ui/common/gradient_button.dart';
@ -19,6 +24,23 @@ class OnboardingPage extends StatefulWidget {
}
class _OnboardingPageState extends State<OnboardingPage> {
late StreamSubscription<TriggerLogoutEvent> _triggerLogoutEvent;
@override
void initState() {
_triggerLogoutEvent =
Bus.instance.on<TriggerLogoutEvent>().listen((event) async {
await autoLogoutAlert(context);
});
super.initState();
}
@override
void dispose() {
_triggerLogoutEvent.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
debugPrint("Building OnboardingPage");

View file

@ -8,6 +8,7 @@ import 'package:ente_auth/core/event_bus.dart';
import 'package:ente_auth/core/network.dart';
import 'package:ente_auth/events/codes_updated_event.dart';
import 'package:ente_auth/events/signed_in_event.dart';
import 'package:ente_auth/events/trigger_logout_event.dart';
import 'package:ente_auth/gateway/authenticator.dart';
import 'package:ente_auth/models/authenticator/auth_entity.dart';
import 'package:ente_auth/models/authenticator/auth_key.dart';
@ -132,6 +133,9 @@ class AuthenticatorService {
await _localToRemoteSync();
_logger.info("local push completed");
Bus.instance.fire(CodesUpdatedEvent());
} on UnauthorizedError {
debugPrint("Firing logout event");
Bus.instance.fire(TriggerLogoutEvent());
} catch (e) {
_logger.severe("Failed to sync with remote", e);
}
@ -140,7 +144,7 @@ class AuthenticatorService {
Future<void> _remoteToLocalSync() async {
_logger.info('Initiating remote to local sync');
final int lastSyncTime = _prefs.getInt(_lastEntitySyncTime) ?? 0;
_logger.info("Current synctime is " + lastSyncTime.toString());
_logger.info("Current sync is " + lastSyncTime.toString());
const int fetchLimit = 500;
final List<AuthEntity> result =
await _gateway.getDiff(lastSyncTime, limit: fetchLimit);

View file

@ -0,0 +1,34 @@
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/utils/dialog_util.dart';
import 'package:flutter/material.dart';
Future<void> autoLogoutAlert(BuildContext context) async {
final AlertDialog alert = AlertDialog(
title: const Text("Session expired"),
content: const Text("Please login again"),
actions: [
TextButton(
child: Text(
"Ok",
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop('dialog');
Navigator.of(context).popUntil((route) => route.isFirst);
final dialog = createProgressDialog(context, "Logging out...");
await dialog.show();
await Configuration.instance.logout();
await dialog.hide();
},
),
],
);
await showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}

View file

@ -7,12 +7,14 @@ import 'dart:ui';
import 'package:ente_auth/core/event_bus.dart';
import 'package:ente_auth/ente_theme_data.dart';
import 'package:ente_auth/events/codes_updated_event.dart';
import 'package:ente_auth/events/trigger_logout_event.dart';
import "package:ente_auth/l10n/l10n.dart";
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/onboarding/view/setup_enter_secret_key_page.dart';
import 'package:ente_auth/services/preference_service.dart';
import 'package:ente_auth/services/user_service.dart';
import 'package:ente_auth/store/code_store.dart';
import 'package:ente_auth/ui/account/logout_dialog.dart';
import 'package:ente_auth/ui/code_widget.dart';
import 'package:ente_auth/ui/common/loading_widget.dart';
import 'package:ente_auth/ui/scanner_page.dart';
@ -43,6 +45,7 @@ class _HomePageState extends State<HomePage> {
List<Code> _codes = [];
List<Code> _filteredCodes = [];
StreamSubscription<CodesUpdatedEvent>? _streamSubscription;
StreamSubscription<TriggerLogoutEvent>? _triggerLogoutEvent;
@override
void initState() {
@ -51,6 +54,10 @@ class _HomePageState extends State<HomePage> {
_streamSubscription = Bus.instance.on<CodesUpdatedEvent>().listen((event) {
_loadCodes();
});
_triggerLogoutEvent =
Bus.instance.on<TriggerLogoutEvent>().listen((event) async {
await autoLogoutAlert(context);
});
super.initState();
}
@ -84,6 +91,7 @@ class _HomePageState extends State<HomePage> {
@override
void dispose() {
_streamSubscription?.cancel();
_triggerLogoutEvent?.cancel();
_textController.removeListener(_applyFiltering);
super.dispose();
}