Better client/login/authState handling
All checks were successful
Build Docker and Deploy / Build Docker Container (push) Successful in 2m58s
Build Docker and Deploy / Deploy to Server (push) Successful in 32s

This commit is contained in:
2024-06-02 17:09:57 +02:00
parent ec506a7f9e
commit ac299ec7ba
17 changed files with 496 additions and 401 deletions

View File

@@ -3,12 +3,13 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/models/client.dart';
import 'package:simplecloudnotifier/nav_layout.dart';
import 'package:simplecloudnotifier/state/app_theme.dart';
import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/request_log.dart';
import 'package:simplecloudnotifier/state/user_account.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:toastification/toastification.dart';
import 'firebase_options.dart';
@@ -41,7 +42,20 @@ void main() async {
ApplicationLog.error('Failed to open Hive-Box: scn-logs: ' + exc.toString(), trace: trace);
}
UserAccount(); // ensure UserAccount is loaded
final appAuth = AppAuth(); // ensure UserAccount is loaded
if (appAuth.isAuth()) {
try {
await appAuth.loadUser();
} catch (exc, trace) {
ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace);
}
try {
await appAuth.loadClient();
} catch (exc, trace) {
ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace);
}
}
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
@@ -71,7 +85,7 @@ void main() async {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => UserAccount(), lazy: false),
ChangeNotifierProvider(create: (context) => AppAuth(), lazy: false),
ChangeNotifierProvider(create: (context) => AppTheme(), lazy: false),
],
child: const SCNApp(),
@@ -80,27 +94,38 @@ void main() async {
}
void setFirebaseToken(String fcmToken) async {
final acc = UserAccount();
final acc = AppAuth();
final oldToken = Globals().getPrefFCMToken();
if (oldToken != null && oldToken == fcmToken && acc.client != null && acc.client!.fcmToken == fcmToken) {
await Globals().setPrefFCMToken(fcmToken);
ApplicationLog.info('New firebase token received', additional: 'Token: $fcmToken (old: $oldToken)');
if (!acc.isAuth()) return;
Client? client;
try {
client = await acc.loadClient(force: true);
} catch (exc, trace) {
ApplicationLog.error('Failed to get client: ' + exc.toString(), trace: trace);
return;
}
if (oldToken != null && oldToken == fcmToken && client != null && client!.fcmToken == fcmToken) {
ApplicationLog.info('Firebase token unchanged - do nothing', additional: 'Token: $fcmToken');
return;
}
ApplicationLog.info('New firebase token received', additional: 'Token: $fcmToken (old: $oldToken)');
await Globals().setPrefFCMToken(fcmToken);
if (acc.auth != null) {
if (acc.client == null) {
final client = await APIClient.addClient(acc.auth, fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
acc.setClient(client);
} else {
final client = await APIClient.updateClient(acc.auth, acc.client!.clientID, fcmToken, Globals().deviceModel, Globals().hostname, Globals().version);
acc.setClient(client);
}
if (client == null) {
// should not really happen - perhaps someone externally deleted the client?
final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
acc.setClientAndClientID(newClient);
await acc.save();
} else {
final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken, Globals().deviceModel, Globals().hostname, Globals().version);
acc.setClientAndClientID(newClient);
await acc.save();
}
}