Improve handling of logout and token expiration
This commit is contained in:
parent
730de37b78
commit
5561f18a2c
6 changed files with 142 additions and 136 deletions
|
@ -14,8 +14,9 @@ class ClientHolder extends InheritedWidget {
|
|||
super.key,
|
||||
required AuthenticationData authenticationData,
|
||||
required this.settingsClient,
|
||||
required super.child
|
||||
}) : apiClient = ApiClient(authenticationData: authenticationData);
|
||||
required super.child,
|
||||
required Function() onLogout,
|
||||
}) : apiClient = ApiClient(authenticationData: authenticationData, onLogout: onLogout);
|
||||
|
||||
static ClientHolder? maybeOf(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<ClientHolder>();
|
||||
|
|
|
@ -18,10 +18,12 @@ class ApiClient {
|
|||
static const String tokenKey = "token";
|
||||
static const String passwordKey = "password";
|
||||
|
||||
ApiClient({required AuthenticationData authenticationData}) : _authenticationData = authenticationData;
|
||||
ApiClient({required AuthenticationData authenticationData, required this.onLogout}) : _authenticationData = authenticationData;
|
||||
|
||||
final AuthenticationData _authenticationData;
|
||||
final Logger _logger = Logger("API");
|
||||
// Saving the context here feels kinda cringe ngl
|
||||
final Function() onLogout;
|
||||
|
||||
AuthenticationData get authenticationData => _authenticationData;
|
||||
String get userId => _authenticationData.userId;
|
||||
|
@ -103,7 +105,7 @@ class ApiClient {
|
|||
return AuthenticationData.unauthenticated();
|
||||
}
|
||||
|
||||
Future<void> logout(BuildContext context) async {
|
||||
Future<void> logout() async {
|
||||
const FlutterSecureStorage storage = FlutterSecureStorage(
|
||||
aOptions: AndroidOptions(encryptedSharedPreferences: true),
|
||||
);
|
||||
|
@ -111,9 +113,7 @@ class ApiClient {
|
|||
await storage.delete(key: machineIdKey);
|
||||
await storage.delete(key: tokenKey);
|
||||
await storage.delete(key: passwordKey);
|
||||
if (context.mounted) {
|
||||
Phoenix.rebirth(context);
|
||||
}
|
||||
onLogout();
|
||||
}
|
||||
|
||||
Future<void> extendSession() async {
|
||||
|
@ -127,7 +127,7 @@ class ApiClient {
|
|||
if (response.statusCode == 403) {
|
||||
tryCachedLogin().then((value) {
|
||||
if (!value.isAuthenticated) {
|
||||
// TODO: Turn api-client into a change notifier to present login screen when logged out
|
||||
onLogout();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ class ApiClient {
|
|||
if (response.statusCode < 300) return;
|
||||
|
||||
final error = "${switch (response.statusCode) {
|
||||
429 => "Sorry, you are being rate limited.",
|
||||
429 => "You are being rate limited.",
|
||||
403 => "You are not authorized to do that.",
|
||||
404 => "Resource not found.",
|
||||
500 => "Internal server error.",
|
||||
|
|
|
@ -15,7 +15,6 @@ class SettingsClient {
|
|||
final data = await _storage.read(key: _settingsKey);
|
||||
if (data == null) return;
|
||||
_currentSettings = Settings.fromMap(jsonDecode(data));
|
||||
|
||||
}
|
||||
|
||||
Future<void> changeSettings(Settings newSettings) async {
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'dart:developer';
|
|||
|
||||
import 'package:contacts_plus_plus/apis/github_api.dart';
|
||||
import 'package:contacts_plus_plus/client_holder.dart';
|
||||
import 'package:contacts_plus_plus/clients/api_client.dart';
|
||||
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||
import 'package:contacts_plus_plus/clients/settings_client.dart';
|
||||
import 'package:contacts_plus_plus/models/sem_ver.dart';
|
||||
|
@ -26,14 +27,20 @@ void main() async {
|
|||
Logger.root.onRecord.listen((event) => log("${dateFormat.format(event.time)}: ${event.message}", name: event.loggerName, time: event.time));
|
||||
final settingsClient = SettingsClient();
|
||||
await settingsClient.loadSettings();
|
||||
await settingsClient.changeSettings(settingsClient.currentSettings); // Save generated defaults to disk
|
||||
runApp(Phoenix(child: ContactsPlusPlus(settingsClient: settingsClient,)));
|
||||
final newSettings = settingsClient.currentSettings.copyWith(machineId: settingsClient.currentSettings.machineId.valueOrDefault);
|
||||
await settingsClient.changeSettings(newSettings); // Save generated machineId to disk
|
||||
AuthenticationData cachedAuth = AuthenticationData.unauthenticated();
|
||||
try {
|
||||
cachedAuth = await ApiClient.tryCachedLogin();
|
||||
} catch (_) {}
|
||||
runApp(ContactsPlusPlus(settingsClient: settingsClient, cachedAuthentication: cachedAuth));
|
||||
}
|
||||
|
||||
class ContactsPlusPlus extends StatefulWidget {
|
||||
const ContactsPlusPlus({required this.settingsClient, super.key});
|
||||
const ContactsPlusPlus({required this.settingsClient, required this.cachedAuthentication, super.key});
|
||||
|
||||
final SettingsClient settingsClient;
|
||||
final AuthenticationData cachedAuthentication;
|
||||
|
||||
@override
|
||||
State<ContactsPlusPlus> createState() => _ContactsPlusPlusState();
|
||||
|
@ -41,7 +48,7 @@ class ContactsPlusPlus extends StatefulWidget {
|
|||
|
||||
class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
|
||||
final Typography _typography = Typography.material2021(platform: TargetPlatform.android);
|
||||
AuthenticationData _authData = AuthenticationData.unauthenticated();
|
||||
late AuthenticationData _authData = widget.cachedAuthentication;
|
||||
bool _checkedForUpdate = false;
|
||||
|
||||
void showUpdateDialogOnFirstBuild(BuildContext context) {
|
||||
|
@ -95,9 +102,18 @@ class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Phoenix(
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
return ClientHolder(
|
||||
settingsClient: widget.settingsClient,
|
||||
authenticationData: _authData,
|
||||
onLogout: () {
|
||||
setState(() {
|
||||
_authData = AuthenticationData.unauthenticated();
|
||||
});
|
||||
Phoenix.rebirth(context);
|
||||
},
|
||||
child: DynamicColorBuilder(
|
||||
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) => MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
|
@ -135,4 +151,7 @@ class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
|
|||
),
|
||||
);
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,7 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||
final TextEditingController _passwordController = TextEditingController();
|
||||
final TextEditingController _totpController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
late final Future<AuthenticationData> _cachedLoginFuture = ApiClient.tryCachedLogin().then((value) async {
|
||||
if (value.isAuthenticated) {
|
||||
await loginSuccessful(value);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
late final FocusNode _passwordFocusNode;
|
||||
late final FocusNode _totpFocusNode;
|
||||
|
||||
|
@ -150,14 +145,8 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||
appBar: AppBar(
|
||||
title: const Text("Contacts++"),
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: _cachedLoginFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData || snapshot.hasError) {
|
||||
final authData = snapshot.data;
|
||||
if (authData?.isAuthenticated ?? false) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
body: Builder(
|
||||
builder: (context) {
|
||||
return ListView(
|
||||
controller: _scrollController,
|
||||
children: [
|
||||
|
@ -245,8 +234,6 @@ class _LoginScreenState extends State<LoginScreen> {
|
|||
],
|
||||
);
|
||||
}
|
||||
return const LinearProgressIndicator();
|
||||
}
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ class SettingsPage extends StatelessWidget {
|
|||
TextButton(onPressed: () => Navigator.of(context).pop(), child: const Text("No")),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await ClientHolder.of(context).apiClient.logout(context);
|
||||
await ClientHolder.of(context).apiClient.logout();
|
||||
},
|
||||
child: const Text("Yes"),
|
||||
),
|
||||
|
|
Loading…
Reference in a new issue