Add migration route for old sharedPrefs

This commit is contained in:
Mike Schwörer 2025-06-07 20:55:42 +02:00
parent 9862fda9e5
commit bafcff7be4
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 48 additions and 0 deletions

View File

@ -92,6 +92,8 @@ void main() async {
print('[INIT] Skip Firebase init (Platform == Linux)...');
}
await appAuth.tryMigrateFromV1();
print('[INIT] Load Notifications...');
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

View File

@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/api/api_exception.dart';
@ -69,6 +70,51 @@ class AppAuth extends ChangeNotifier implements TokenSource {
notifyListeners();
}
Future<bool> tryMigrateFromV1() async {
try {
final sp = Globals().sharedPrefs;
if (sp.containsKey("auth.userid") || !sp.containsKey("user_id") || !sp.containsKey("user_key")) {
return false;
}
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken == null) {
ApplicationLog.error('Failed to migrate from v1: No FCM Token');
return false;
}
// migrate from v1
final oldUserID = sp.getString('user_id');
final oldUserKey = sp.getString('user_key');
if (oldUserID == null || oldUserKey == null || oldUserID.isEmpty || oldUserKey.isEmpty) {
ApplicationLog.error('Failed to migrate from v1: user_id or user_key is null|empty');
return false;
}
final newTokenSend = await APIClient.createKeyToken(DirectTokenSource(oldUserID, oldUserKey), "SendKey (auto-migration to SCN v2)", "CS", true);
final user = await APIClient.getUser(DirectTokenSource(oldUserID, oldUserKey), oldUserID);
final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
set(user, client, oldUserKey, newTokenSend.token);
sp.remove('user_id');
sp.remove('user_key');
ApplicationLog.info('Migrated prefs from v1 to v2', additional: 'UserID: ${oldUserID}, ClientID: ${client.clientID}, TokenAdmin: ${oldUserKey}, TokenSend: ${newTokenSend.token}');
return true;
} catch (exc, trace) {
ApplicationLog.error('Failed to migrate from v1: ' + exc.toString(), trace: trace);
return false;
}
}
void load() {
//final cdat = Globals().sharedPrefs.getString('auth.cdate');
//final mdat = Globals().sharedPrefs.getString('auth.mdate');