KyrosDev Claude commited on
Commit
6b7637e
·
1 Parent(s): 101cfda

修復 AuthManager 初始化時機問題

Browse files

問題修復:
- 解決 AuthManager 初始化時 Supabase 尚未載入的問題
- 修復「依賴未準備好」錯誤導致的重複重定向

技術改進:
- 新增 waitForDependenciesAndInit 方法等待依賴載入
- 等待 Supabase 和配置都載入完成才初始化
- 登入頁面改用定期檢查方式等待 AuthManager
- 防止登入頁面被錯誤重定向

穩定性提升:
- 確保初始化順序正確
- 避免競態條件
- 提供合理的超時機制

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (2) hide show
  1. frontend/js/auth.js +52 -7
  2. frontend/login.html +13 -6
frontend/js/auth.js CHANGED
@@ -31,8 +31,8 @@ class AuthManager {
31
  this.ACTIVITY_TIMEOUT = 15 * 60 * 1000; // 15分鐘
32
  this.WARNING_TIME = 2 * 60 * 1000; // 最後2分鐘顯示警告
33
  console.log('🔧 AuthManager 創建 - 安全模式(15分鐘無活動+關閉瀏覽器自動登出)');
34
- // 立即初始化,零延遲
35
- this.init();
36
  }
37
  }
38
 
@@ -69,14 +69,59 @@ class AuthManager {
69
  });
70
  }
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  async init() {
73
  try {
74
- console.log('🚀 AuthManager 簡化初始化');
75
-
76
- // 簡單檢查依賴
77
  if (!window.supabase?.createClient || !window.APP_CONFIG?.CONFIG_LOADED) {
78
- console.log('⚠️ 依賴未準備好,重定向到登入');
79
- return this.redirectToLogin();
 
 
 
 
80
  }
81
 
82
  // 創建 Supabase 客戶端
 
31
  this.ACTIVITY_TIMEOUT = 15 * 60 * 1000; // 15分鐘
32
  this.WARNING_TIME = 2 * 60 * 1000; // 最後2分鐘顯示警告
33
  console.log('🔧 AuthManager 創建 - 安全模式(15分鐘無活動+關閉瀏覽器自動登出)');
34
+ // 等待 Supabase 和配置載入後初始化
35
+ this.waitForDependenciesAndInit();
36
  }
37
  }
38
 
 
69
  });
70
  }
71
 
72
+ async waitForDependenciesAndInit() {
73
+ console.log('🔄 等待依賴載入...');
74
+
75
+ // 等待 Supabase 載入
76
+ if (!window.supabase?.createClient) {
77
+ await new Promise(resolve => {
78
+ const checkSupabase = setInterval(() => {
79
+ if (window.supabase?.createClient) {
80
+ clearInterval(checkSupabase);
81
+ resolve();
82
+ }
83
+ }, 50);
84
+ // 最多等 3 秒
85
+ setTimeout(() => {
86
+ clearInterval(checkSupabase);
87
+ resolve();
88
+ }, 3000);
89
+ });
90
+ }
91
+
92
+ // 等待配置載入
93
+ if (!window.APP_CONFIG?.CONFIG_LOADED) {
94
+ await new Promise(resolve => {
95
+ const checkConfig = setInterval(() => {
96
+ if (window.APP_CONFIG?.CONFIG_LOADED) {
97
+ clearInterval(checkConfig);
98
+ resolve();
99
+ }
100
+ }, 50);
101
+ // 最多等 3 秒
102
+ setTimeout(() => {
103
+ clearInterval(checkConfig);
104
+ resolve();
105
+ }, 3000);
106
+ });
107
+ }
108
+
109
+ // 依賴就緒後初始化
110
+ await this.init();
111
+ }
112
+
113
  async init() {
114
  try {
115
+ console.log('🚀 AuthManager 初始化');
116
+
117
+ // 再次檢查依賴(防程式)
118
  if (!window.supabase?.createClient || !window.APP_CONFIG?.CONFIG_LOADED) {
119
+ console.log('⚠️ 依賴未準備好,重定向到登入');
120
+ // 如果是登入頁面,不重定向
121
+ if (!window.location.pathname.includes('login.html')) {
122
+ return this.redirectToLogin();
123
+ }
124
+ return;
125
  }
126
 
127
  // 創建 Supabase 客戶端
frontend/login.html CHANGED
@@ -150,16 +150,23 @@
150
  // 簡化登入頁面初始化
151
  document.addEventListener('DOMContentLoaded', () => {
152
  console.log('🔄 登入頁面初始化');
153
-
154
- // 等待 AuthManager 準備好
155
- setTimeout(() => {
156
  if (window.authManager?.initialized) {
157
  console.log('✅ 初始化登入表單');
158
  window.authManager.initLoginPage();
159
- } else {
160
- console.log('⚠️ AuthManager 尚未準備好');
 
 
 
 
 
 
 
161
  }
162
- }, 3000);
163
  });
164
  </script>
165
  </body>
 
150
  // 簡化登入頁面初始化
151
  document.addEventListener('DOMContentLoaded', () => {
152
  console.log('🔄 登入頁面初始化');
153
+
154
+ // 持續檢查 AuthManager 是否準備好
155
+ const checkAuthManager = setInterval(() => {
156
  if (window.authManager?.initialized) {
157
  console.log('✅ 初始化登入表單');
158
  window.authManager.initLoginPage();
159
+ clearInterval(checkAuthManager);
160
+ }
161
+ }, 100);
162
+
163
+ // 最多等待 5 秒
164
+ setTimeout(() => {
165
+ clearInterval(checkAuthManager);
166
+ if (!window.authManager?.initialized) {
167
+ console.log('⚠️ AuthManager 初始化超時');
168
  }
169
+ }, 5000);
170
  });
171
  </script>
172
  </body>