整合資料庫、完成登入註冊忘記密碼功能
This commit is contained in:
78
scripts/test-forgot-password-new-flow.js
Normal file
78
scripts/test-forgot-password-new-flow.js
Normal file
@@ -0,0 +1,78 @@
|
||||
async function testForgotPasswordNewFlow() {
|
||||
console.log('🧪 測試新的忘記密碼流程...\n');
|
||||
|
||||
try {
|
||||
// 1. 測試忘記密碼 API
|
||||
console.log('1. 測試忘記密碼 API...');
|
||||
const response = await fetch('http://localhost:3000/api/auth/forgot-password', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: 'admin@ai-platform.com'
|
||||
})
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
console.log('✅ 忘記密碼 API 測試成功');
|
||||
console.log('生成的重設連結:', data.resetUrl);
|
||||
console.log('過期時間:', data.expiresAt);
|
||||
|
||||
// 解析 URL 參數
|
||||
const url = new URL(data.resetUrl);
|
||||
const token = url.searchParams.get('token');
|
||||
const email = url.searchParams.get('email');
|
||||
const mode = url.searchParams.get('mode');
|
||||
const name = url.searchParams.get('name');
|
||||
const department = url.searchParams.get('department');
|
||||
|
||||
console.log('\n📋 URL 參數解析:');
|
||||
console.log('- token:', token);
|
||||
console.log('- email:', email);
|
||||
console.log('- mode:', mode);
|
||||
console.log('- name:', name);
|
||||
console.log('- department:', department);
|
||||
|
||||
// 2. 測試密碼重設 API
|
||||
console.log('\n2. 測試密碼重設 API...');
|
||||
const resetResponse = await fetch('http://localhost:3000/api/auth/reset-password', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: token,
|
||||
password: 'newpassword123'
|
||||
})
|
||||
});
|
||||
|
||||
if (resetResponse.ok) {
|
||||
const resetData = await resetResponse.json();
|
||||
console.log('✅ 密碼重設 API 測試成功:', resetData);
|
||||
} else {
|
||||
const errorData = await resetResponse.text();
|
||||
console.log('❌ 密碼重設 API 測試失敗:', resetResponse.status, errorData);
|
||||
}
|
||||
|
||||
} else {
|
||||
const errorData = await response.text();
|
||||
console.log('❌ 忘記密碼 API 測試失敗:', response.status, errorData);
|
||||
}
|
||||
|
||||
console.log('\n🎉 新流程測試完成!');
|
||||
console.log('\n📝 使用方式:');
|
||||
console.log('1. 用戶點擊「忘記密碼」');
|
||||
console.log('2. 輸入電子郵件地址');
|
||||
console.log('3. 系統生成一次性重設連結');
|
||||
console.log('4. 用戶複製連結並在新視窗中開啟');
|
||||
console.log('5. 在註冊頁面設定新密碼');
|
||||
console.log('6. 完成密碼重設');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 測試過程中發生錯誤:', error);
|
||||
}
|
||||
}
|
||||
|
||||
testForgotPasswordNewFlow();
|
Reference in New Issue
Block a user