-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-auth.html
More file actions
190 lines (167 loc) · 5.03 KB
/
test-auth.html
File metadata and controls
190 lines (167 loc) · 5.03 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authentication Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.test-section {
border: 1px solid #ddd;
border-radius: 5px;
padding: 20px;
margin-bottom: 20px;
}
.test-result {
margin-top: 10px;
padding: 10px;
border-radius: 5px;
}
.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
input {
width: 100%;
padding: 8px;
margin: 5px 0;
box-sizing: border-box;
border: 1px solid #ddd;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Authentication System Test</h1>
<div class="test-section">
<h2>Test Login</h2>
<p>Use the test credentials to verify the authentication system:</p>
<p><strong>Email:</strong> test@example.com</p>
<p><strong>Password:</strong> password123</p>
<div>
<label for="email">Email:</label>
<input type="email" id="email" value="test@example.com">
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" value="password123">
</div>
<button onclick="testLogin()">Test Login</button>
<div id="login-result" class="test-result" style="display: none;"></div>
</div>
<div class="test-section">
<h2>Check Authentication Status</h2>
<button onclick="checkAuth()">Check Authentication</button>
<div id="auth-result" class="test-result" style="display: none;"></div>
</div>
<div class="test-section">
<h2>Logout</h2>
<button onclick="testLogout()">Logout</button>
<div id="logout-result" class="test-result" style="display: none;"></div>
</div>
<script>
function showMessage(elementId, message, isSuccess) {
const element = document.getElementById(elementId);
element.textContent = message;
element.className = 'test-result ' + (isSuccess ? 'success' : 'error');
element.style.display = 'block';
}
function testLogin() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Store session ID in localStorage
localStorage.setItem('sessionId', data.sessionId);
showMessage('login-result', 'Login successful! Session ID: ' + data.sessionId, true);
} else {
showMessage('login-result', 'Login failed: ' + data.message, false);
}
})
.catch(error => {
showMessage('login-result', 'Error: ' + error.message, false);
});
}
function checkAuth() {
const sessionId = localStorage.getItem('sessionId');
if (!sessionId) {
showMessage('auth-result', 'No session found. Please login first.', false);
return;
}
fetch('/api/auth/check', {
headers: {
'X-Session-Id': sessionId
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
showMessage('auth-result', 'Authenticated as: ' + data.user.name + ' (' + data.user.email + ')', true);
} else {
showMessage('auth-result', 'Not authenticated: ' + data.message, false);
}
})
.catch(error => {
showMessage('auth-result', 'Error: ' + error.message, false);
});
}
function testLogout() {
const sessionId = localStorage.getItem('sessionId');
if (!sessionId) {
showMessage('logout-result', 'No session found.', false);
return;
}
fetch('/api/logout', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ sessionId: sessionId })
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Clear session from localStorage
localStorage.removeItem('sessionId');
showMessage('logout-result', 'Logout successful!', true);
} else {
showMessage('logout-result', 'Logout failed: ' + data.message, false);
}
})
.catch(error => {
showMessage('logout-result', 'Error: ' + error.message, false);
});
}
</script>
</body>
</html>