feat(client): 登录体验 — 记住邮箱 + 密码显隐 + 7天免登陆
1. 记住邮箱:登录页预填上次邮箱(TokenStore.saveLastEmail,登录/注册成功时存, 退出登录不清除)。 2. 密码显隐:登录/注册密码框加眼睛按钮切换明文(PangolinIcons.eye/eyeOff)。 3. 7天免登陆:RootFlow 改为响应 authProvider——启动时有有效会话直接进主界面; AuthNotifier.refresh() 在服务端拒绝(refresh 过期/超 7 天)时 logout 回登录页, 网络错误则保留会话。会话恢复期间显示极简启动屏。 flutter analyze 0 error;114 tests passed。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+26
-14
@@ -7,8 +7,10 @@ import 'pangolin_theme.dart';
|
||||
import 'services/token_store.dart';
|
||||
import 'shell/home_shell.dart';
|
||||
import 'state/app_providers.dart';
|
||||
import 'state/auth_provider.dart';
|
||||
import 'widgets/auth_screen.dart';
|
||||
import 'widgets/onboarding_screen.dart';
|
||||
import 'widgets/pangolin_logo.dart';
|
||||
|
||||
void main() => runApp(const ProviderScope(child: PangolinApp()));
|
||||
|
||||
@@ -29,9 +31,8 @@ class PangolinApp extends ConsumerWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// 应用阶段:登录 → 引导 → 主应用
|
||||
enum AppStage { auth, onboarding, app }
|
||||
|
||||
/// 应用根流转:启动时若已有有效会话(7 天内 refresh 有效)直接进主界面,
|
||||
/// 免登陆;会话失效则回登录页。引导页仅首次注册/登录后看一次。
|
||||
class RootFlow extends ConsumerStatefulWidget {
|
||||
const RootFlow({super.key});
|
||||
@override
|
||||
@@ -39,32 +40,43 @@ class RootFlow extends ConsumerStatefulWidget {
|
||||
}
|
||||
|
||||
class _RootFlowState extends ConsumerState<RootFlow> {
|
||||
AppStage stage = AppStage.auth;
|
||||
final _store = const TokenStore();
|
||||
bool _forceOnboarding = false;
|
||||
|
||||
// 登录后:引导只首次看过一次;之后直接进主界面。
|
||||
// 登录/注册成功后:未看过引导才进引导,否则直接主界面。
|
||||
Future<void> _afterAuth() async {
|
||||
final onboarded = await _store.isOnboarded();
|
||||
if (!mounted) return;
|
||||
setState(() => stage = onboarded ? AppStage.app : AppStage.onboarding);
|
||||
setState(() => _forceOnboarding = !onboarded);
|
||||
}
|
||||
|
||||
Future<void> _afterOnboarding() async {
|
||||
await _store.markOnboarded();
|
||||
if (!mounted) return;
|
||||
setState(() => stage = AppStage.app);
|
||||
setState(() => _forceOnboarding = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final t = ref.watch(appTextProvider);
|
||||
switch (stage) {
|
||||
case AppStage.auth:
|
||||
return AuthScreen(t: t, onDone: () { _afterAuth(); });
|
||||
case AppStage.onboarding:
|
||||
return OnboardingScreen(t: t, onDone: () { _afterOnboarding(); });
|
||||
case AppStage.app:
|
||||
return const HomeShell();
|
||||
final auth = ref.watch(authProvider);
|
||||
|
||||
if (auth.isLoading) return const _Splash();
|
||||
if (!auth.isLoggedIn) {
|
||||
return AuthScreen(t: t, onDone: () { _afterAuth(); });
|
||||
}
|
||||
if (_forceOnboarding) {
|
||||
return OnboardingScreen(t: t, onDone: () { _afterOnboarding(); });
|
||||
}
|
||||
return const HomeShell();
|
||||
}
|
||||
}
|
||||
|
||||
/// 会话恢复期间的极简启动屏(避免登录页一闪而过)。
|
||||
class _Splash extends StatelessWidget {
|
||||
const _Splash();
|
||||
@override
|
||||
Widget build(BuildContext context) => const Scaffold(
|
||||
body: Center(child: PangolinMark(size: 56)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ class TokenStore {
|
||||
static const _kAccess = 'pangolin_access_token';
|
||||
static const _kRefresh = 'pangolin_refresh_token';
|
||||
static const _kOnboarded = 'pangolin_onboarded';
|
||||
static const _kLastEmail = 'pangolin_last_email';
|
||||
|
||||
Future<void> saveTokens({
|
||||
required String access,
|
||||
@@ -34,4 +35,8 @@ class TokenStore {
|
||||
// 首次引导标记:引导页只在第一次看(看完置位,后续登录直接进主界面)。
|
||||
Future<void> markOnboarded() => _storage.write(key: _kOnboarded, value: '1');
|
||||
Future<bool> isOnboarded() async => (await _storage.read(key: _kOnboarded)) == '1';
|
||||
|
||||
// 记住上次登录邮箱(登录页预填;退出登录不清除)。
|
||||
Future<void> saveLastEmail(String email) => _storage.write(key: _kLastEmail, value: email);
|
||||
Future<String?> loadLastEmail() => _storage.read(key: _kLastEmail);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,11 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
||||
try {
|
||||
await saveTokens(await _api.refresh(rt));
|
||||
return true;
|
||||
} on AuthApiException catch (e) {
|
||||
// 服务端明确拒绝(refresh 过期/失效,超出 7 天)→ 退出登录回登录页;
|
||||
// 网络错误(statusCode<=0)则保留会话,稍后重试。
|
||||
if (e.statusCode > 0) await logout();
|
||||
return false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ class _AuthScreenState extends ConsumerState<AuthScreen> {
|
||||
int _step = 0;
|
||||
bool _sent = false;
|
||||
bool _loading = false;
|
||||
bool _pwVisible = false;
|
||||
String? _errorZh;
|
||||
|
||||
final _email = TextEditingController();
|
||||
@@ -42,6 +43,17 @@ class _AuthScreenState extends ConsumerState<AuthScreen> {
|
||||
|
||||
bool get _emailValid => RegExp(r'\S+@\S+\.\S+').hasMatch(_email.text);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// 预填上次登录邮箱。
|
||||
ref.read(tokenStoreProvider).loadLastEmail().then((email) {
|
||||
if (email != null && email.isNotEmpty && mounted) {
|
||||
setState(() => _email.text = email);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_email.dispose();
|
||||
@@ -71,6 +83,7 @@ class _AuthScreenState extends ConsumerState<AuthScreen> {
|
||||
code: _code.text.trim(),
|
||||
password: _pw.text,
|
||||
);
|
||||
await ref.read(tokenStoreProvider).saveLastEmail(_email.text.trim());
|
||||
await ref.read(authProvider.notifier).saveTokens(tokens);
|
||||
if (mounted) widget.onDone();
|
||||
} on AuthApiException catch (e) {
|
||||
@@ -85,6 +98,7 @@ class _AuthScreenState extends ConsumerState<AuthScreen> {
|
||||
email: _email.text.trim(),
|
||||
password: _pw.text,
|
||||
);
|
||||
await ref.read(tokenStoreProvider).saveLastEmail(_email.text.trim());
|
||||
await ref.read(authProvider.notifier).saveTokens(tokens);
|
||||
if (mounted) widget.onDone();
|
||||
} on AuthApiException catch (e) {
|
||||
@@ -182,13 +196,31 @@ class _AuthScreenState extends ConsumerState<AuthScreen> {
|
||||
InputDecoration get _bare =>
|
||||
const InputDecoration(border: InputBorder.none, isCollapsed: true, contentPadding: EdgeInsets.symmetric(vertical: 12));
|
||||
|
||||
// 密码输入 + 显示/隐藏切换(眼睛按钮)。
|
||||
Widget _pwInput(PangolinScheme c, String hint) => Row(children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _pw,
|
||||
obscureText: !_pwVisible,
|
||||
decoration: _bare.copyWith(hintText: hint),
|
||||
onChanged: (_) => setState(() {}),
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _pwVisible = !_pwVisible),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 6),
|
||||
child: Icon(_pwVisible ? PangolinIcons.eyeOff : PangolinIcons.eye, size: 18, color: c.fg3),
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
Widget _login(PangolinScheme c, AppText t) {
|
||||
return Column(children: [
|
||||
_field(c, icon: PangolinIcons.mail, label: t.emailLabel,
|
||||
child: TextField(controller: _email, decoration: _bare.copyWith(hintText: t.emailPh), onChanged: (_) => setState(() {}))),
|
||||
const SizedBox(height: 16),
|
||||
_field(c, icon: PangolinIcons.lock, label: t.pwLabel,
|
||||
child: TextField(controller: _pw, obscureText: true, decoration: _bare.copyWith(hintText: t.pwPh), onChanged: (_) => setState(() {}))),
|
||||
_field(c, icon: PangolinIcons.lock, label: t.pwLabel, child: _pwInput(c, t.pwPh)),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Padding(
|
||||
@@ -239,8 +271,7 @@ class _AuthScreenState extends ConsumerState<AuthScreen> {
|
||||
return Column(children: [
|
||||
Row(children: [Icon(PangolinIcons.checkCircle, size: 15, color: c.success), const SizedBox(width: 7), Text(_email.text, style: PangolinText.sm.copyWith(color: c.fg2))]),
|
||||
const SizedBox(height: 16),
|
||||
_field(c, icon: PangolinIcons.lock, label: t.pwLabel,
|
||||
child: TextField(controller: _pw, obscureText: true, decoration: _bare.copyWith(hintText: t.setPwPh), onChanged: (_) => setState(() {}))),
|
||||
_field(c, icon: PangolinIcons.lock, label: t.pwLabel, child: _pwInput(c, t.setPwPh)),
|
||||
const SizedBox(height: 18),
|
||||
PangolinButton(
|
||||
label: _loading ? '...' : t.doCreate,
|
||||
|
||||
@@ -18,6 +18,8 @@ class PangolinIcons {
|
||||
|
||||
// ── 导航 / 操作 ──
|
||||
static const settings = LucideIcons.settings;
|
||||
static const eye = LucideIcons.eye;
|
||||
static const eyeOff = LucideIcons.eyeOff;
|
||||
static const user = LucideIcons.user;
|
||||
static const search = LucideIcons.search;
|
||||
static const check = LucideIcons.check;
|
||||
|
||||
@@ -30,6 +30,10 @@ class _NullTokenStore implements TokenStore {
|
||||
Future<void> markOnboarded() async {}
|
||||
@override
|
||||
Future<bool> isOnboarded() async => true;
|
||||
@override
|
||||
Future<void> saveLastEmail(String email) async {}
|
||||
@override
|
||||
Future<String?> loadLastEmail() async => null;
|
||||
}
|
||||
|
||||
Future<void> _shoot(WidgetTester tester, NavView view, String name) async {
|
||||
|
||||
@@ -29,6 +29,10 @@ class _NullTokenStore implements TokenStore {
|
||||
Future<void> markOnboarded() async {}
|
||||
@override
|
||||
Future<bool> isOnboarded() async => true;
|
||||
@override
|
||||
Future<void> saveLastEmail(String email) async {}
|
||||
@override
|
||||
Future<String?> loadLastEmail() async => null;
|
||||
}
|
||||
|
||||
// ── 辅助 ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -23,6 +23,10 @@ class _NullTokenStore implements TokenStore {
|
||||
Future<void> markOnboarded() async {}
|
||||
@override
|
||||
Future<bool> isOnboarded() async => true;
|
||||
@override
|
||||
Future<void> saveLastEmail(String email) async {}
|
||||
@override
|
||||
Future<String?> loadLastEmail() async => null;
|
||||
}
|
||||
|
||||
// 直接喂固定节点列表(不触发真实探针/HTTP)。
|
||||
|
||||
@@ -20,6 +20,10 @@ class _NullTokenStore implements TokenStore {
|
||||
Future<void> markOnboarded() async {}
|
||||
@override
|
||||
Future<bool> isOnboarded() async => true;
|
||||
@override
|
||||
Future<void> saveLastEmail(String email) async {}
|
||||
@override
|
||||
Future<String?> loadLastEmail() async => null;
|
||||
}
|
||||
|
||||
ProviderContainer _container() => ProviderContainer(
|
||||
|
||||
Reference in New Issue
Block a user