Files
ai-showcase-platform/scripts/test-frontend-login.js

84 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

async function testFrontendLogin() {
console.log('🧪 測試前端登入狀態...\n');
try {
// 1. 測試登入 API
console.log('1. 測試登入 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('用戶角色:', loginData.user?.role);
// 2. 測試用戶資料 API
console.log('\n2. 測試用戶資料 API...');
const profileResponse = await fetch('http://localhost:3000/api/auth/profile', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// 注意:這裡沒有包含認證 token因為我們沒有實現 JWT
}
});
console.log('用戶資料 API 狀態:', profileResponse.status);
if (profileResponse.ok) {
const profileData = await profileResponse.json();
console.log('✅ 用戶資料 API 成功');
console.log('用戶角色:', profileData.role);
} else {
console.log('❌ 用戶資料 API 失敗');
}
// 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 if (pageContent.includes('儀表板') || pageContent.includes('管理員')) {
console.log('✅ 管理員頁面正常顯示');
} else {
console.log('⚠️ 頁面內容不確定');
}
}
} else {
const errorData = await loginResponse.text();
console.log('❌ 登入 API 失敗:', loginResponse.status, errorData);
}
console.log('\n🎉 前端登入狀態測試完成!');
console.log('\n💡 建議:');
console.log('1. 檢查瀏覽器中的 localStorage 是否有用戶資料');
console.log('2. 確認登入後用戶狀態是否正確更新');
console.log('3. 檢查權限檢查邏輯是否正確');
} catch (error) {
console.error('❌ 測試過程中發生錯誤:', error);
}
}
testFrontendLogin();