From 463e7ee287754ec5034cac232c93c113d687a9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 11 Feb 2024 02:20:48 +0100 Subject: [PATCH] persistance --- flutter/devtools_options.yaml | 1 + flutter/lib/pages/account/login.dart | 44 ++++-- flutter/lib/pages/account/root.dart | 21 +-- flutter/lib/state/user_account.dart | 37 ++++- .../Flutter/GeneratedPluginRegistrant.swift | 2 + flutter/pubspec.lock | 135 +++++++++++++++++- flutter/pubspec.yaml | 1 + 7 files changed, 217 insertions(+), 24 deletions(-) create mode 100644 flutter/devtools_options.yaml diff --git a/flutter/devtools_options.yaml b/flutter/devtools_options.yaml new file mode 100644 index 0000000..7e7e7f6 --- /dev/null +++ b/flutter/devtools_options.yaml @@ -0,0 +1 @@ +extensions: diff --git a/flutter/lib/pages/account/login.dart b/flutter/lib/pages/account/login.dart index 1f974ec..7f5ed27 100644 --- a/flutter/lib/pages/account/login.dart +++ b/flutter/lib/pages/account/login.dart @@ -1,8 +1,13 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:simplecloudnotifier/api/api_client.dart'; +import 'package:simplecloudnotifier/models/key_token_auth.dart'; +import 'package:simplecloudnotifier/state/user_account.dart'; class AccountLoginPage extends StatefulWidget { - const AccountLoginPage({super.key}); + final void Function()? onLogin; + + const AccountLoginPage({super.key, this.onLogin}); @override State createState() => _AccountLoginPageState(); @@ -66,20 +71,31 @@ class _AccountLoginPageState extends State { void _login() async { final msgr = ScaffoldMessenger.of(context); + final prov = Provider.of(context, listen: false); - final verified = await APIClient.verifyToken(_ctrlUserID.text, _ctrlToken.text); - if (verified) { - msgr.showSnackBar( - const SnackBar( - content: Text('Data ok'), - ), - ); - } else { - msgr.showSnackBar( - const SnackBar( - content: Text('Failed to verify token'), - ), - ); + try { + final uid = _ctrlUserID.text; + final tok = _ctrlToken.text; + + final verified = await APIClient.verifyToken(uid, tok); + if (verified) { + msgr.showSnackBar( + const SnackBar( + content: Text('Data ok'), //TODO toast + ), + ); + prov.setToken(KeyTokenAuth(userId: uid, token: tok)); + await prov.save(); + widget.onLogin?.call(); + } else { + msgr.showSnackBar( + const SnackBar( + content: Text('Failed to verify token'), //TODO toast + ), + ); + } + } catch (e) { + //TODO } } } diff --git a/flutter/lib/pages/account/root.dart b/flutter/lib/pages/account/root.dart index e5feb98..ff6a1f6 100644 --- a/flutter/lib/pages/account/root.dart +++ b/flutter/lib/pages/account/root.dart @@ -16,21 +16,22 @@ enum _SubPage { chooseAuth, login, main } class _AccountRootPageState extends State { late _SubPage _page; + late UserAccount userAcc; @override void initState() { super.initState(); - var prov = Provider.of(context, listen: false); + userAcc = Provider.of(context, listen: false); - _page = (prov.auth != null) ? _SubPage.main : _SubPage.chooseAuth; + _page = (userAcc.auth != null) ? _SubPage.main : _SubPage.chooseAuth; - prov.addListener(_onAuthStateChanged); + userAcc.addListener(_onAuthStateChanged); } @override void dispose() { - Provider.of(context, listen: false).removeListener(_onAuthStateChanged); + userAcc.removeListener(_onAuthStateChanged); super.dispose(); } @@ -48,10 +49,10 @@ class _AccountRootPageState extends State { builder: (context, acc, child) { switch (_page) { case _SubPage.main: - return const Center( + return Center( child: Text( - 'Logged In', - style: TextStyle(fontSize: 24), + 'Logged In: ${acc.auth?.userId}', + style: const TextStyle(fontSize: 24), ), ); case _SubPage.chooseAuth: @@ -64,7 +65,11 @@ class _AccountRootPageState extends State { }), ); case _SubPage.login: - return const AccountLoginPage(); + return AccountLoginPage( + onLogin: () => setState(() { + _page = _SubPage.main; + }), + ); } }, ); diff --git a/flutter/lib/state/user_account.dart b/flutter/lib/state/user_account.dart index 7cad39c..deaf2f7 100644 --- a/flutter/lib/state/user_account.dart +++ b/flutter/lib/state/user_account.dart @@ -1,4 +1,5 @@ import 'package:flutter/foundation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import '../models/key_token_auth.dart'; import '../models/user.dart'; @@ -10,9 +11,19 @@ class UserAccount extends ChangeNotifier { KeyTokenAuth? _auth; KeyTokenAuth? get auth => _auth; + UserAccount() { + load(); + } + void setToken(KeyTokenAuth auth) { _auth = auth; - _user = user; + _user = null; + notifyListeners(); + } + + void clearToken() { + _auth = null; + _user = null; notifyListeners(); } @@ -25,4 +36,28 @@ class UserAccount extends ChangeNotifier { _user = null; notifyListeners(); } + + load() async { + final prefs = await SharedPreferences.getInstance(); + + final uid = prefs.getString('auth.userid'); + final tok = prefs.getString('auth.token'); + + if (uid != null && tok != null) { + setToken(KeyTokenAuth(userId: uid, token: tok)); + } else { + clearToken(); + } + } + + save() async { + final prefs = await SharedPreferences.getInstance(); + if (_auth == null) { + await prefs.remove('auth.userid'); + await prefs.remove('auth.token'); + } else { + await prefs.setString('auth.userid', _auth!.userId); + await prefs.setString('auth.token', _auth!.token); + } + } } diff --git a/flutter/macos/Flutter/GeneratedPluginRegistrant.swift b/flutter/macos/Flutter/GeneratedPluginRegistrant.swift index cccf817..724bb2a 100644 --- a/flutter/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/flutter/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,8 @@ import FlutterMacOS import Foundation +import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/flutter/pubspec.lock b/flutter/pubspec.lock index f667ed0..ecf6a8f 100644 --- a/flutter/pubspec.lock +++ b/flutter/pubspec.lock @@ -57,6 +57,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + ffi: + dependency: transitive + description: + name: ffi + sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878" + url: "https://pub.dev" + source: hosted + version: "2.1.0" + file: + dependency: transitive + description: + name: file + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + url: "https://pub.dev" + source: hosted + version: "7.0.0" flutter: dependency: "direct main" description: flutter @@ -75,6 +91,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" font_awesome_flutter: dependency: "direct main" description: @@ -146,6 +167,46 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170" + url: "https://pub.dev" + source: hosted + version: "2.2.1" + platform: + dependency: transitive + description: + name: platform + sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec" + url: "https://pub.dev" + source: hosted + version: "3.1.4" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.dev" + source: hosted + version: "2.1.8" provider: dependency: "direct main" description: @@ -154,6 +215,62 @@ packages: url: "https://pub.dev" source: hosted version: "6.1.1" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02" + url: "https://pub.dev" + source: hosted + version: "2.2.2" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06" + url: "https://pub.dev" + source: hosted + version: "2.2.1" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "7708d83064f38060c7b39db12aefe449cb8cdc031d6062280087bc4cdb988f5c" + url: "https://pub.dev" + source: hosted + version: "2.3.5" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: "7b15ffb9387ea3e237bb7a66b8a23d2147663d391cafc5c8f37b2e7b4bde5d21" + url: "https://pub.dev" + source: hosted + version: "2.2.2" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59" + url: "https://pub.dev" + source: hosted + version: "2.3.2" sky_engine: dependency: transitive description: flutter @@ -231,6 +348,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.3.0" + win32: + dependency: transitive + description: + name: win32 + sha256: "464f5674532865248444b4c3daca12bd9bf2d7c47f759ce2617986e7229494a8" + url: "https://pub.dev" + source: hosted + version: "5.2.0" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d + url: "https://pub.dev" + source: hosted + version: "1.0.4" sdks: dart: ">=3.2.6 <4.0.0" - flutter: ">=1.16.0" + flutter: ">=3.16.0" diff --git a/flutter/pubspec.yaml b/flutter/pubspec.yaml index deea4e0..16baa53 100644 --- a/flutter/pubspec.yaml +++ b/flutter/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: cupertino_icons: ^1.0.2 http: ^1.2.0 provider: ^6.1.1 + shared_preferences: ^2.2.2 dependency_overrides: