persistance
This commit is contained in:
parent
46897cc51b
commit
463e7ee287
1
flutter/devtools_options.yaml
Normal file
1
flutter/devtools_options.yaml
Normal file
@ -0,0 +1 @@
|
||||
extensions:
|
@ -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<AccountLoginPage> createState() => _AccountLoginPageState();
|
||||
@ -66,20 +71,31 @@ class _AccountLoginPageState extends State<AccountLoginPage> {
|
||||
|
||||
void _login() async {
|
||||
final msgr = ScaffoldMessenger.of(context);
|
||||
final prov = Provider.of<UserAccount>(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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,21 +16,22 @@ enum _SubPage { chooseAuth, login, main }
|
||||
|
||||
class _AccountRootPageState extends State<AccountRootPage> {
|
||||
late _SubPage _page;
|
||||
late UserAccount userAcc;
|
||||
|
||||
@override
|
||||
void 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
|
||||
void dispose() {
|
||||
Provider.of<UserAccount>(context, listen: false).removeListener(_onAuthStateChanged);
|
||||
userAcc.removeListener(_onAuthStateChanged);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@ -48,10 +49,10 @@ class _AccountRootPageState extends State<AccountRootPage> {
|
||||
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<AccountRootPage> {
|
||||
}),
|
||||
);
|
||||
case _SubPage.login:
|
||||
return const AccountLoginPage();
|
||||
return AccountLoginPage(
|
||||
onLogin: () => setState(() {
|
||||
_page = _SubPage.main;
|
||||
}),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@
|
||||
import FlutterMacOS
|
||||
import Foundation
|
||||
|
||||
import shared_preferences_foundation
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user