forked from alexyuetmout/template-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-auth.js
More file actions
94 lines (83 loc) · 2.58 KB
/
test-auth.js
File metadata and controls
94 lines (83 loc) · 2.58 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 测试Better Auth功能的简单脚本
// 可以在浏览器开发者工具中运行
console.log('🧪 测试Better Auth功能');
// 测试1: 检查authClient是否可用
try {
const { updateUser, setPassword, useSession } = window.authClient || {};
console.log('✅ authClient可用:', !!updateUser && !!setPassword);
} catch (e) {
console.log('❌ authClient不可用:', e.message);
}
// 测试2: 检查会话状态
async function testSession() {
try {
const response = await fetch('/api/auth/session');
const session = await response.json();
console.log('📋 当前会话状态:', session);
return session;
} catch (e) {
console.log('❌ 无法获取会话:', e.message);
return null;
}
}
// 测试3: 测试更新用户信息API
async function testUpdateProfile(name) {
try {
const response = await fetch('/api/auth/update-user', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name }),
});
const result = await response.json();
console.log('📝 更新用户信息结果:', result);
return result;
} catch (e) {
console.log('❌ 更新用户信息失败:', e.message);
return null;
}
}
// 测试4: 测试设置密码API
async function testSetPassword(password) {
try {
const response = await fetch('/api/auth/set-password', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ password }),
});
const result = await response.json();
console.log('🔐 设置密码结果:', result);
return result;
} catch (e) {
console.log('❌ 设置密码失败:', e.message);
return null;
}
}
// 运行测试
async function runTests() {
console.log('\n🚀 开始运行测试...\n');
const session = await testSession();
if (session && session.user) {
console.log('\n👤 用户已登录,测试更新功能...');
await testUpdateProfile('测试用户' + Date.now());
await testSetPassword('TestPassword123');
} else {
console.log('\n🔒 用户未登录,请先登录后再运行测试');
}
console.log('\n✨ 测试完成!');
}
// 导出测试函数
window.testAuth = {
runTests,
testSession,
testUpdateProfile,
testSetPassword
};
console.log('💡 使用方法:');
console.log(' testAuth.runTests() - 运行所有测试');
console.log(' testAuth.testSession() - 检查登录状态');
console.log(' testAuth.testUpdateProfile("新名字") - 测试更新用户名');
console.log(' testAuth.testSetPassword("新密码") - 测试设置密码');