persistance

This commit is contained in:
Mike Schwörer 2024-02-11 02:20:48 +01:00
parent 46897cc51b
commit 463e7ee287
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
7 changed files with 217 additions and 24 deletions

View File

@ -0,0 +1 @@
extensions:

View File

@ -1,8 +1,13 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.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 { class AccountLoginPage extends StatefulWidget {
const AccountLoginPage({super.key}); final void Function()? onLogin;
const AccountLoginPage({super.key, this.onLogin});
@override @override
State<AccountLoginPage> createState() => _AccountLoginPageState(); State<AccountLoginPage> createState() => _AccountLoginPageState();
@ -66,20 +71,31 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
void _login() async { void _login() async {
final msgr = ScaffoldMessenger.of(context); final msgr = ScaffoldMessenger.of(context);
final prov = Provider.of<UserAccount>(context, listen: false);
final verified = await APIClient.verifyToken(_ctrlUserID.text, _ctrlToken.text); try {
if (verified) { final uid = _ctrlUserID.text;
msgr.showSnackBar( final tok = _ctrlToken.text;
const SnackBar(
content: Text('Data ok'), final verified = await APIClient.verifyToken(uid, tok);
), if (verified) {
); msgr.showSnackBar(
} else { const SnackBar(
msgr.showSnackBar( content: Text('Data ok'), //TODO toast
const SnackBar( ),
content: Text('Failed to verify token'), );
), 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
} }
} }
} }

View File

@ -16,21 +16,22 @@ enum _SubPage { chooseAuth, login, main }
class _AccountRootPageState extends State<AccountRootPage> { class _AccountRootPageState extends State<AccountRootPage> {
late _SubPage _page; late _SubPage _page;
late UserAccount userAcc;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
var prov = Provider.of<UserAccount>(context, listen: false); userAcc = Provider.of<UserAccount>(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 @override
void dispose() { void dispose() {
Provider.of<UserAccount>(context, listen: false).removeListener(_onAuthStateChanged); userAcc.removeListener(_onAuthStateChanged);
super.dispose(); super.dispose();
} }
@ -48,10 +49,10 @@ class _AccountRootPageState extends State<AccountRootPage> {
builder: (context, acc, child) { builder: (context, acc, child) {
switch (_page) { switch (_page) {
case _SubPage.main: case _SubPage.main:
return const Center( return Center(
child: Text( child: Text(
'Logged In', 'Logged In: ${acc.auth?.userId}',
style: TextStyle(fontSize: 24), style: const TextStyle(fontSize: 24),
), ),
); );
case _SubPage.chooseAuth: case _SubPage.chooseAuth:
@ -64,7 +65,11 @@ class _AccountRootPageState extends State<AccountRootPage> {
}), }),
); );
case _SubPage.login: case _SubPage.login:
return const AccountLoginPage(); return AccountLoginPage(
onLogin: () => setState(() {
_page = _SubPage.main;
}),
);
} }
}, },
); );

View File

@ -1,4 +1,5 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/key_token_auth.dart'; import '../models/key_token_auth.dart';
import '../models/user.dart'; import '../models/user.dart';
@ -10,9 +11,19 @@ class UserAccount extends ChangeNotifier {
KeyTokenAuth? _auth; KeyTokenAuth? _auth;
KeyTokenAuth? get auth => _auth; KeyTokenAuth? get auth => _auth;
UserAccount() {
load();
}
void setToken(KeyTokenAuth auth) { void setToken(KeyTokenAuth auth) {
_auth = auth; _auth = auth;
_user = user; _user = null;
notifyListeners();
}
void clearToken() {
_auth = null;
_user = null;
notifyListeners(); notifyListeners();
} }
@ -25,4 +36,28 @@ class UserAccount extends ChangeNotifier {
_user = null; _user = null;
notifyListeners(); 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);
}
}
} }

View File

@ -5,6 +5,8 @@
import FlutterMacOS import FlutterMacOS
import Foundation import Foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
} }

View File

@ -57,6 +57,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.1" 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: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -75,6 +91,11 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
font_awesome_flutter: font_awesome_flutter:
dependency: "direct main" dependency: "direct main"
description: description:
@ -146,6 +167,46 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.8.3" 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: provider:
dependency: "direct main" dependency: "direct main"
description: description:
@ -154,6 +215,62 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.1.1" 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: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -231,6 +348,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.3.0" 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: sdks:
dart: ">=3.2.6 <4.0.0" dart: ">=3.2.6 <4.0.0"
flutter: ">=1.16.0" flutter: ">=3.16.0"

View File

@ -38,6 +38,7 @@ dependencies:
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
http: ^1.2.0 http: ^1.2.0
provider: ^6.1.1 provider: ^6.1.1
shared_preferences: ^2.2.2
dependency_overrides: dependency_overrides: