mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
Check token info and update to new info
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'package:logging/logging.dart';
|
||||
import 'api/status.dart';
|
||||
import 'api/token.dart';
|
||||
import 'api/user.dart';
|
||||
import 'globals.dart';
|
||||
import 'platform/device.dart';
|
||||
|
||||
final _log = Logger("AuthInfo");
|
||||
|
||||
@@ -11,6 +13,8 @@ class AuthInfo {
|
||||
BUser? get user => _user;
|
||||
ServerStatus? _status;
|
||||
ServerStatus? get status => _status;
|
||||
Token? _token;
|
||||
Token? get token => _token;
|
||||
bool get isAuthed => (_user != null);
|
||||
bool _checked = false;
|
||||
bool get checked => _checked;
|
||||
@@ -29,6 +33,42 @@ class AuthInfo {
|
||||
_status = (await api.getStatus()).unwrap();
|
||||
}
|
||||
|
||||
Future<void> checkSessionInfo() async {
|
||||
final data = (await api.getToken()).unwrap();
|
||||
_token = data.token;
|
||||
final d = await device;
|
||||
final cv = await clientVersion;
|
||||
final cp = clientPlatform;
|
||||
String? client;
|
||||
String? ed;
|
||||
String? ecv;
|
||||
String? ecp;
|
||||
if (_token!.client != "flutter") {
|
||||
client = "flutter";
|
||||
}
|
||||
if (_token!.device != d) {
|
||||
ed = d;
|
||||
}
|
||||
if (_token!.clientVersion != cv) {
|
||||
ecv = cv;
|
||||
}
|
||||
if (_token!.clientPlatform != cp) {
|
||||
ecp = cp;
|
||||
}
|
||||
if (client != null || ed != null || ecv != null || ecp != null) {
|
||||
try {
|
||||
final re = await api.updateToken(
|
||||
client: client,
|
||||
device: ed,
|
||||
clientVersion: ecv,
|
||||
clientPlatform: ecp);
|
||||
_token = re.unwrap();
|
||||
} catch (e) {
|
||||
_log.warning("Failed to update token:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> checkAuth() async {
|
||||
_isChecking = true;
|
||||
try {
|
||||
@@ -46,6 +86,7 @@ class AuthInfo {
|
||||
}
|
||||
_checked = true;
|
||||
await getServerStatus();
|
||||
await checkSessionInfo();
|
||||
return re.ok;
|
||||
} finally {
|
||||
_isChecking = false;
|
||||
|
||||
Reference in New Issue
Block a user