♻️ Replace storage token engine to prevent some platform specific issue

This commit is contained in:
LittleSheep 2024-11-23 19:54:38 +08:00
parent dfd80021b9
commit 3cd08da3b6
11 changed files with 53 additions and 83 deletions

View File

@ -1,11 +1,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:dio_smart_retry/dio_smart_retry.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:surface/providers/adapters/sn_network_universal.dart';
import 'package:synchronized/synchronized.dart';
const kAtkStoreKey = 'nex_user_atk';
const kRtkStoreKey = 'nex_user_rtk';
@ -23,7 +24,6 @@ class SnNetworkProvider {
late Dio client;
late final SharedPreferences _prefs;
late final FlutterSecureStorage _storage = FlutterSecureStorage();
SnNetworkProvider() {
client = Dio();
@ -62,9 +62,19 @@ class SnNetworkProvider {
});
}
final tkLock = Lock();
Completer<String?>? _refreshCompleter;
Future<String?> getFreshAtk() async {
if (_refreshCompleter != null) {
return await _refreshCompleter!.future;
} else {
_refreshCompleter = Completer<String?>();
}
try {
var atk = await _storage.read(key: kAtkStoreKey);
var atk = _prefs.getString(kAtkStoreKey);
if (atk != null) {
final atkParts = atk.split('.');
if (atkParts.length != 3) {
@ -94,13 +104,18 @@ class SnNetworkProvider {
}
if (atk != null) {
_refreshCompleter!.complete(atk);
return atk;
} else {
log('Access token refresh failed...');
_refreshCompleter!.complete(null);
}
}
} catch (err) {
log('Failed to authenticate user: $err');
_refreshCompleter!.completeError(err);
} finally {
_refreshCompleter = null;
}
return null;
@ -111,22 +126,18 @@ class SnNetworkProvider {
return '${client.options.baseUrl}/cgi/uc/attachments/$ky';
}
Future<void> setTokenPair(String atk, String rtk) async {
await Future.wait([
_storage.write(key: kAtkStoreKey, value: atk),
_storage.write(key: kRtkStoreKey, value: rtk),
]);
void setTokenPair(String atk, String rtk) {
_prefs.setString(kAtkStoreKey, atk);
_prefs.setString(kRtkStoreKey, rtk);
}
Future<void> clearTokenPair() async {
await Future.wait([
_storage.delete(key: kAtkStoreKey),
_storage.delete(key: kRtkStoreKey),
]);
void clearTokenPair() {
_prefs.remove(kAtkStoreKey);
_prefs.remove(kRtkStoreKey);
}
Future<String?> refreshToken() async {
final rtk = await _storage.read(key: kRtkStoreKey);
final rtk = _prefs.getString(kRtkStoreKey);
if (rtk == null) return null;
final dio = Dio();
@ -139,7 +150,7 @@ class SnNetworkProvider {
final atk = resp.data['access_token'];
final nRtk = resp.data['refresh_token'];
await setTokenPair(atk, nRtk);
setTokenPair(atk, nRtk);
return atk;
}

View File

@ -1,8 +1,8 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:surface/providers/sn_network.dart';
import 'package:surface/types/account.dart';
@ -11,14 +11,17 @@ class UserProvider extends ChangeNotifier {
SnAccount? user;
late final SnNetworkProvider _sn;
late final FlutterSecureStorage _storage = FlutterSecureStorage();
Future<String?> get atk => _storage.read(key: kAtkStoreKey);
Future<String?> get atk async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(kAtkStoreKey);
}
UserProvider(BuildContext context) {
_sn = context.read<SnNetworkProvider>();
_storage.read(key: kAtkStoreKey).then((value) {
SharedPreferences.getInstance().then((prefs) {
final value = prefs.getString(kAtkStoreKey);
isAuthorized = value != null;
notifyListeners();
refreshUser().then((value) {
@ -41,7 +44,7 @@ class UserProvider extends ChangeNotifier {
}
void logoutUser() async {
await _sn.clearTokenPair();
_sn.clearTokenPair();
isAuthorized = false;
user = null;
notifyListeners();

View File

@ -151,7 +151,7 @@ class _LoginCheckScreenState extends State<_LoginCheckScreen> {
});
final atk = tokenResp.data['access_token'];
final rtk = tokenResp.data['refresh_token'];
await sn.setTokenPair(atk, rtk);
sn.setTokenPair(atk, rtk);
if (!mounted) return;
final user = context.read<UserProvider>();
final userinfo = await user.refreshUser();

View File

@ -74,6 +74,15 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
if (mounted) setState(() => _isBusy = false);
}
void _showAdvancedSearchTune() {
showModalBottomSheet(
context: context,
builder: (context) => Column(
children: [],
),
);
}
@override
Widget build(BuildContext context) {
const labelShadows = <Shadow>[
@ -87,11 +96,17 @@ class _PostSearchScreenState extends State<PostSearchScreen> {
return Scaffold(
appBar: AppBar(
title: Text('screenPostSearch').tr(),
actions: [
IconButton(
icon: const Icon(Symbols.tune),
onPressed: _showAdvancedSearchTune,
),
],
),
body: Stack(
children: [
InfiniteList(
padding: const EdgeInsets.only(top: 96),
padding: const EdgeInsets.only(top: 100),
itemCount: _posts.length,
isLoading: _isBusy,
hasReachedMax: _postCount != null && _posts.length >= _postCount!,

View File

@ -7,7 +7,6 @@
#include "generated_plugin_registrant.h"
#include <file_selector_linux/file_selector_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <flutter_udid/flutter_udid_plugin.h>
#include <isar_flutter_libs/isar_flutter_libs_plugin.h>
#include <media_kit_libs_linux/media_kit_libs_linux_plugin.h>
@ -20,9 +19,6 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
g_autoptr(FlPluginRegistrar) flutter_udid_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterUdidPlugin");
flutter_udid_plugin_register_with_registrar(flutter_udid_registrar);

View File

@ -4,7 +4,6 @@
list(APPEND FLUTTER_PLUGIN_LIST
file_selector_linux
flutter_secure_storage_linux
flutter_udid
isar_flutter_libs
media_kit_libs_linux

View File

@ -10,7 +10,6 @@ import file_selector_macos
import firebase_analytics
import firebase_core
import firebase_messaging
import flutter_secure_storage_macos
import flutter_udid
import isar_flutter_libs
import media_kit_libs_macos_video
@ -31,7 +30,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseAnalyticsPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAnalyticsPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
FlutterUdidPlugin.register(with: registry.registrar(forPlugin: "FlutterUdidPlugin"))
IsarFlutterLibsPlugin.register(with: registry.registrar(forPlugin: "IsarFlutterLibsPlugin"))
MediaKitLibsMacosVideoPlugin.register(with: registry.registrar(forPlugin: "MediaKitLibsMacosVideoPlugin"))

View File

@ -608,54 +608,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.23"
flutter_secure_storage:
dependency: "direct main"
description:
name: flutter_secure_storage
sha256: "165164745e6afb5c0e3e3fcc72a012fb9e58496fb26ffb92cf22e16a821e85d0"
url: "https://pub.dev"
source: hosted
version: "9.2.2"
flutter_secure_storage_linux:
dependency: transitive
description:
name: flutter_secure_storage_linux
sha256: "4d91bfc23047422cbcd73ac684bc169859ee766482517c22172c86596bf1464b"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_macos:
dependency: transitive
description:
name: flutter_secure_storage_macos
sha256: "1693ab11121a5f925bbea0be725abfcfbbcf36c1e29e571f84a0c0f436147a81"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_secure_storage_platform_interface:
dependency: transitive
description:
name: flutter_secure_storage_platform_interface
sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8
url: "https://pub.dev"
source: hosted
version: "1.1.2"
flutter_secure_storage_web:
dependency: transitive
description:
name: flutter_secure_storage_web
sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_windows:
dependency: transitive
description:
name: flutter_secure_storage_windows
sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_shaders:
dependency: transitive
description:
@ -1608,7 +1560,7 @@ packages:
source: hosted
version: "1.0.6"
synchronized:
dependency: transitive
dependency: "direct main"
description:
name: synchronized
sha256: "69fe30f3a8b04a0be0c15ae6490fc859a78ef4c43ae2dd5e8a623d45bfcf9225"

View File

@ -58,7 +58,6 @@ dependencies:
google_fonts: ^6.2.1
path: ^1.9.0
relative_time: ^5.0.0
flutter_secure_storage: ^9.2.2
image_picker: ^1.1.2
cross_file: ^0.3.4+2
file_picker: ^8.1.3
@ -88,6 +87,7 @@ dependencies:
media_kit_libs_video: ^1.0.5
pasteboard: ^0.3.0
sentry_flutter: ^8.10.1
synchronized: ^3.3.0+3
dev_dependencies:
flutter_test:

View File

@ -9,7 +9,6 @@
#include <connectivity_plus/connectivity_plus_windows_plugin.h>
#include <file_selector_windows/file_selector_windows.h>
#include <firebase_core/firebase_core_plugin_c_api.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <flutter_udid/flutter_udid_plugin_c_api.h>
#include <isar_flutter_libs/isar_flutter_libs_plugin.h>
#include <media_kit_libs_windows_video/media_kit_libs_windows_video_plugin_c_api.h>
@ -26,8 +25,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("FileSelectorWindows"));
FirebaseCorePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
FlutterUdidPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterUdidPluginCApi"));
IsarFlutterLibsPluginRegisterWithRegistrar(

View File

@ -6,7 +6,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
connectivity_plus
file_selector_windows
firebase_core
flutter_secure_storage_windows
flutter_udid
isar_flutter_libs
media_kit_libs_windows_video