94 lines
3.3 KiB
JavaScript
94 lines
3.3 KiB
JavaScript
async function testCompleteLoginFlow() {
|
|
console.log('🧪 測試完整登入流程...\n');
|
|
|
|
try {
|
|
// 1. 測試首頁(登入頁面)
|
|
console.log('1. 測試首頁(登入頁面)...');
|
|
const homeResponse = await fetch('http://localhost:3000/');
|
|
|
|
if (homeResponse.ok) {
|
|
console.log('✅ 首頁載入成功');
|
|
const homeContent = await homeResponse.text();
|
|
|
|
if (homeContent.includes('登入') || homeContent.includes('Login')) {
|
|
console.log('✅ 首頁包含登入功能');
|
|
} else {
|
|
console.log('⚠️ 首頁可能不包含登入功能');
|
|
}
|
|
} else {
|
|
console.log('❌ 首頁載入失敗:', homeResponse.status);
|
|
}
|
|
|
|
// 2. 測試管理員登入 API
|
|
console.log('\n2. 測試管理員登入 API...');
|
|
const loginResponse = await fetch('http://localhost:3000/api/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
email: 'admin@ai-platform.com',
|
|
password: 'admin123456'
|
|
})
|
|
});
|
|
|
|
if (loginResponse.ok) {
|
|
const loginData = await loginResponse.json();
|
|
console.log('✅ 管理員登入 API 成功');
|
|
console.log('用戶資料:', {
|
|
id: loginData.user?.id,
|
|
name: loginData.user?.name,
|
|
email: loginData.user?.email,
|
|
role: loginData.user?.role
|
|
});
|
|
} else {
|
|
const errorData = await loginResponse.text();
|
|
console.log('❌ 管理員登入 API 失敗:', loginResponse.status, errorData);
|
|
return;
|
|
}
|
|
|
|
// 3. 測試管理員頁面(未登入狀態)
|
|
console.log('\n3. 測試管理員頁面(未登入狀態)...');
|
|
const adminResponse = await fetch('http://localhost:3000/admin');
|
|
|
|
if (adminResponse.ok) {
|
|
const pageContent = await adminResponse.text();
|
|
|
|
if (pageContent.includes('存取被拒')) {
|
|
console.log('✅ 管理員頁面正確顯示存取被拒(未登入)');
|
|
|
|
// 檢查調試信息
|
|
const debugMatch = pageContent.match(/調試信息: 用戶=([^,]+), 角色=([^<]+)/);
|
|
if (debugMatch) {
|
|
console.log('📋 調試信息:', {
|
|
用戶: debugMatch[1],
|
|
角色: debugMatch[2]
|
|
});
|
|
} else {
|
|
console.log('⚠️ 沒有調試信息');
|
|
}
|
|
} else if (pageContent.includes('載入中')) {
|
|
console.log('❌ 管理員頁面顯示載入中(應該顯示存取被拒)');
|
|
} else if (pageContent.includes('儀表板') || pageContent.includes('管理員')) {
|
|
console.log('⚠️ 管理員頁面直接顯示內容(可能沒有權限檢查)');
|
|
} else {
|
|
console.log('⚠️ 管理員頁面內容不確定');
|
|
}
|
|
} else {
|
|
console.log('❌ 管理員頁面載入失敗:', adminResponse.status);
|
|
}
|
|
|
|
console.log('\n🎉 完整登入流程測試完成!');
|
|
console.log('\n💡 結論:');
|
|
console.log('1. 管理員頁面需要先登入才能訪問');
|
|
console.log('2. 未登入時顯示「存取被拒」是正確的行為');
|
|
console.log('3. 用戶需要通過前端登入界面登入');
|
|
console.log('4. 登入後才能正常訪問管理員後台');
|
|
|
|
} catch (error) {
|
|
console.error('❌ 測試過程中發生錯誤:', error);
|
|
}
|
|
}
|
|
|
|
testCompleteLoginFlow();
|