8d890c0424
- 新增 lib/core/network/:ApiClient(dio 封装,错误信封→类型化异常,base URL 经 --dart-define=PATBOND_API_BASE_URL 注入)、AuthInterceptor(Bearer + 设备标识)、TokenRefresher(单飞刷新,401/40101 重放一次,仅 40102/再 401 清会话) - 新增 lib/features/auth/:SessionManager(token 只进 flutter_secure_storage)、AuthRepository(login/register/refresh/logout/me,注册带 Idempotency-Key)、Splash(500ms 最短停留/5s 超时/错误态重试+改用账号登录)、登录页与注册页(照 12 号组装稿,错误三层映射,预留区不渲染) - App 根组件改为认证状态机驱动(Splash↔登录↔主壳 300ms fade);个人中心退出登录接入真实 logout - 顺带修复:FIX-1 促销卡渐变改 [primaryStrong, primary]、FIX-2 补 helperStyle: muted、README dart format 命令补 --output=none - 新增依赖:dio ^5.11.1、flutter_secure_storage ^11.0.0、uuid ^4.6.0 - 门禁:dart format(0 changed)/ flutter analyze(No issues)/ flutter test(30 passed,其中新增 23:TokenRefresher 5 + AuthRepository 10 + 登录页 4 + 注册页 4) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
160 lines
5.2 KiB
Dart
160 lines
5.2 KiB
Dart
import 'package:flutter/foundation.dart';
|
||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||
import 'package:patbond_flutter/features/auth/auth_models.dart';
|
||
import 'package:uuid/uuid.dart';
|
||
|
||
/// 认证状态机:unknown(启动恢复中,停留 Splash)→ authenticated / unauthenticated。
|
||
enum AuthStatus { unknown, authenticated, unauthenticated }
|
||
|
||
/// token 持久化抽象。生产实现走系统安全存储(Keychain / Keystore),
|
||
/// 测试注入内存实现。token 绝不写入 SharedPreferences(开发计划 §4.2)。
|
||
abstract class TokenStore {
|
||
Future<String?> read(String key);
|
||
Future<void> write(String key, String value);
|
||
Future<void> delete(String key);
|
||
}
|
||
|
||
/// 基于 flutter_secure_storage 的生产实现。
|
||
class SecureTokenStore implements TokenStore {
|
||
const SecureTokenStore([this._storage = const FlutterSecureStorage()]);
|
||
|
||
final FlutterSecureStorage _storage;
|
||
|
||
@override
|
||
Future<String?> read(String key) => _storage.read(key: key);
|
||
|
||
@override
|
||
Future<void> write(String key, String value) =>
|
||
_storage.write(key: key, value: value);
|
||
|
||
@override
|
||
Future<void> delete(String key) => _storage.delete(key: key);
|
||
}
|
||
|
||
/// 会话管理:token 的内存副本 + 安全存储持久化 + 认证状态广播。
|
||
///
|
||
/// 状态变更只通过 [saveSession](登录/注册成功)、[markAuthenticated] /
|
||
/// [markUnauthenticated](Splash 恢复裁决)与 [clearSession](登出/会话失效)。
|
||
/// TokenRefresher 轮换 token 用 [updateTokens],不改状态——
|
||
/// Splash 需要在最短停留时间后才切页(12 号组装稿 §7)。
|
||
class SessionManager extends ChangeNotifier {
|
||
SessionManager({required this._store, this._uuid = const Uuid()});
|
||
|
||
static const _accessTokenKey = 'patbond_access_token';
|
||
static const _refreshTokenKey = 'patbond_refresh_token';
|
||
static const _accessExpiresKey = 'patbond_access_token_expires_at';
|
||
static const _refreshExpiresKey = 'patbond_refresh_token_expires_at';
|
||
static const _userIdKey = 'patbond_user_id';
|
||
static const _deviceIdKey = 'patbond_device_id';
|
||
|
||
final TokenStore _store;
|
||
final Uuid _uuid;
|
||
|
||
AuthStatus _status = AuthStatus.unknown;
|
||
String? _accessToken;
|
||
String? _refreshToken;
|
||
String? _userId;
|
||
String? _deviceId;
|
||
bool _loaded = false;
|
||
|
||
AuthStatus get status => _status;
|
||
String? get accessToken => _accessToken;
|
||
String? get refreshToken => _refreshToken;
|
||
String? get userId => _userId;
|
||
|
||
/// 设备标识:首次生成 UUID 并持久化,跨会话稳定。
|
||
String? get deviceId => _deviceId;
|
||
|
||
/// 从安全存储载入 token 到内存(幂等)。存储读取失败按无会话处理,
|
||
/// 不阻塞启动。
|
||
Future<void> loadFromStorage() async {
|
||
if (_loaded) return;
|
||
_accessToken = await _readOrNull(_accessTokenKey);
|
||
_refreshToken = await _readOrNull(_refreshTokenKey);
|
||
_userId = await _readOrNull(_userIdKey);
|
||
_deviceId = await _readOrNull(_deviceIdKey);
|
||
if (_deviceId == null) {
|
||
_deviceId = _uuid.v4();
|
||
await _writeQuietly(_deviceIdKey, _deviceId!);
|
||
}
|
||
_loaded = true;
|
||
}
|
||
|
||
/// 登录/注册成功:持久化整套 token 并进入已认证态。
|
||
Future<void> saveSession(AuthTokens tokens) async {
|
||
await updateTokens(tokens);
|
||
_status = AuthStatus.authenticated;
|
||
notifyListeners();
|
||
}
|
||
|
||
/// 仅写入/轮换 token,不改变认证状态(刷新流程用)。
|
||
Future<void> updateTokens(AuthTokens tokens) async {
|
||
_accessToken = tokens.accessToken;
|
||
_refreshToken = tokens.refreshToken;
|
||
_userId = tokens.userId;
|
||
_loaded = true;
|
||
await _writeQuietly(_accessTokenKey, tokens.accessToken);
|
||
await _writeQuietly(_refreshTokenKey, tokens.refreshToken);
|
||
await _writeQuietly(
|
||
_accessExpiresKey,
|
||
tokens.accessTokenExpiresAt.toIso8601String(),
|
||
);
|
||
await _writeQuietly(
|
||
_refreshExpiresKey,
|
||
tokens.refreshTokenExpiresAt.toIso8601String(),
|
||
);
|
||
await _writeQuietly(_userIdKey, tokens.userId);
|
||
}
|
||
|
||
/// Splash 恢复成功后的显式切态。
|
||
void markAuthenticated() {
|
||
_status = AuthStatus.authenticated;
|
||
notifyListeners();
|
||
}
|
||
|
||
/// 无本地会话或用户选择改用账号登录。
|
||
void markUnauthenticated() {
|
||
_status = AuthStatus.unauthenticated;
|
||
notifyListeners();
|
||
}
|
||
|
||
/// 清除会话(登出 / refresh 失效)。设备标识保留。
|
||
Future<void> clearSession() async {
|
||
_accessToken = null;
|
||
_refreshToken = null;
|
||
_userId = null;
|
||
_status = AuthStatus.unauthenticated;
|
||
notifyListeners();
|
||
for (final key in const [
|
||
_accessTokenKey,
|
||
_refreshTokenKey,
|
||
_accessExpiresKey,
|
||
_refreshExpiresKey,
|
||
_userIdKey,
|
||
]) {
|
||
try {
|
||
await _store.delete(key);
|
||
} catch (error) {
|
||
debugPrint('清除会话存储失败($key):$error');
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<String?> _readOrNull(String key) async {
|
||
try {
|
||
return await _store.read(key);
|
||
} catch (error) {
|
||
debugPrint('读取安全存储失败($key):$error');
|
||
return null;
|
||
}
|
||
}
|
||
|
||
Future<void> _writeQuietly(String key, String value) async {
|
||
try {
|
||
await _store.write(key, value);
|
||
} catch (error) {
|
||
debugPrint('写入安全存储失败($key):$error');
|
||
}
|
||
}
|
||
}
|