修复token过期不跳转登录页:统一401和业务0002拦截,全局导航跳转
This commit is contained in:
@@ -24,6 +24,9 @@ import 'ui/pages/auth/login_page.dart';
|
||||
import 'ui/pages/main/main_page.dart';
|
||||
import 'ui/pages/onboarding/onboarding_page.dart';
|
||||
|
||||
/// 全局導航 Key,用於 token 過期時全局跳轉登錄頁
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
void main() async {
|
||||
// 確保 Flutter 綁定初始化
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -72,7 +75,7 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
|
||||
List<SingleChildWidget> _buildProviders() {
|
||||
final dioClient = DioClient();
|
||||
final dioClient = DioClient(navigatorKey: navigatorKey);
|
||||
|
||||
return [
|
||||
// Theme Provider (必須放在最前面)
|
||||
@@ -93,7 +96,7 @@ class MyApp extends StatelessWidget {
|
||||
create: (ctx) {
|
||||
final authProvider = AuthProvider(ctx.read<UserService>());
|
||||
// token 過期時,DioClient 回調 AuthProvider 強制登出
|
||||
dioClient.onUnauthorized = authProvider.forceLogout;
|
||||
dioClient.onForceLogout = authProvider.forceLogout;
|
||||
return authProvider;
|
||||
},
|
||||
),
|
||||
@@ -112,6 +115,7 @@ class MyApp extends StatelessWidget {
|
||||
|
||||
Widget _buildMaterialApp(BuildContext context, ThemeMode themeMode) {
|
||||
return MaterialApp(
|
||||
navigatorKey: navigatorKey,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: AppTheme.lightTheme,
|
||||
darkTheme: AppTheme.darkTheme,
|
||||
|
||||
Reference in New Issue
Block a user