-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-test.html
More file actions
73 lines (62 loc) · 2.84 KB
/
quick-test.html
File metadata and controls
73 lines (62 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>快速测试 Token</title>
<style>
body { font-family: Arial; max-width: 800px; margin: 0 auto; padding: 20px; }
button { padding: 10px 20px; margin: 10px 0; cursor: pointer; }
#result { background: #f5f5f5; padding: 20px; border-radius: 5px; white-space: pre-wrap; }
.success { color: green; }
.error { color: red; }
</style>
</head>
<body>
<h1>快速测试 Token 访问 API</h1>
<button onclick="quickTest()">一键测试</button>
<button onclick="clearToken()">清除 Token</button>
<div id="result"></div>
<script>
async function quickTest() {
const resultDiv = document.getElementById('result');
// 1. 获取 token
const token = localStorage.getItem('token');
if (!token) {
resultDiv.innerHTML = '<span class="error">❌ 没有找到 token!\n\n请先访问 OAuth 登录页面进行登录。</span>';
return;
}
resultDiv.innerHTML = '正在测试...\n\n';
resultDiv.innerHTML += `Token: ${token.substring(0, 30)}...\n\n`;
try {
// 2. 测试 API
const response = await fetch('http://localhost:3001/api/auth/me', {
headers: {
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
if (response.ok) {
resultDiv.innerHTML += '<span class="success">✅ 成功获取用户信息!\n\n</span>';
resultDiv.innerHTML += '用户数据:\n' + JSON.stringify(data, null, 2);
} else {
resultDiv.innerHTML += '<span class="error">❌ 获取失败!\n\n</span>';
resultDiv.innerHTML += '错误信息:\n' + JSON.stringify(data, null, 2);
if (response.status === 401) {
resultDiv.innerHTML += '\n\n需要重新登录获取新的 token。';
}
}
} catch (error) {
resultDiv.innerHTML += '<span class="error">❌ 请求失败!\n\n</span>';
resultDiv.innerHTML += '错误: ' + error.message;
}
}
function clearToken() {
localStorage.removeItem('token');
localStorage.removeItem('refreshToken');
document.getElementById('result').innerHTML = '✅ Token 已清除,请重新登录。';
}
// 页面加载时自动测试
window.onload = quickTest;
</script>
</body>
</html>